native icon indicating copy to clipboard operation
native copied to clipboard

Add NSAttributedString for object c binding

Open alihassan143 opened this issue 1 month ago • 11 comments

// ignore_for_file: avoid_print

import 'dart:io';

import 'package:ffigen/ffigen.dart';
import 'package:logging/logging.dart';

void main() {
  final iosSdkPath =
      '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks';

  final generator = FfiGenerator(
    headers: Headers(
      entryPoints: [
        // UIKit and PDFKit from real SDK
        Uri.file('$iosSdkPath/UIKit.framework/Headers/UIKit.h'),
        Uri.file('$iosSdkPath/PDFKit.framework/Headers/PDFKit.h'),
        Uri.file('$iosSdkPath/CoreGraphics.framework/Headers/CoreGraphics.h'),
        Uri.file('$iosSdkPath/Foundation.framework/Headers/Foundation.h'),
      ],
    ),
    objectiveC: ObjectiveC(
      interfaces: Interfaces(
        include: (decl) {
          // Include relevant UIKit + PDFKit classes
          return [
            // UIKit
            "UIView", "UILabel", "UIColor", "UIFont", "UIImage", "UIScreen",
            "UINavigationController", "UIWindow", "UIViewController",
            "UIGraphicsImageRenderer", "UIGraphicsImageRendererContext",
            // PDFKit
            "PDFDocument", "PDFPage", "PDFAnnotation", "PDFOutline",
          ].contains(decl.originalName);
        },
        renameMember: (declaration, member) {
          // Handle naming conflicts if any
          if (member == 'initWithFrame:primaryAction:') {
            return 'initWithFramePrimaryAction';
          }
          return member;
        },
      ),
    ),
    output: Output(
      dartFile: Uri.file('lib/system_bindings.g.dart'),
      objectiveCFile: Uri.file('ios/Classes/system_bindings.m'),
      format: true,
      preamble: '''
// ignore_for_file: unused_element, unused_field, return_of_invalid_type

''',
    ),
  );

  generator.generate(
    logger: Logger('Bindings')..onRecord.listen((r) => print(r.message)),
  );

  // Replace many #imports with clean framework imports
  final objcFile = File('ios/Classes/system_bindings.m');
  var content = objcFile.readAsStringSync();
  content = content.replaceAllMapped(
    RegExp(r'#import "(.*\.h)"\n'),
    (match) => '',
  );
  content = '''#import <UIKit/UIKit.h>
#import <PDFKit/PDFKit.h>
#import <CoreGraphics/CoreGraphics.h>
#import <Foundation/Foundation.h>
$content''';
  objcFile.writeAsStringSync(content);

  print('✅ Generated bindings successfully at lib/system_bindings.g.dart');
}

all binding are generated but NSAtttributedString in missing in Objectc also enums values are also not converted to int automatically

alihassan143 avatar Oct 21 '25 05:10 alihassan143