UILabel的attributedText属性设置了CTRunDelegate占位,但是不起作用呢

图片描述

代码如下:

 CGFloat ascentCallback(void* refCon) {
        return 20;
    }

CGFloat descentCallback(void* refCon) {
    return 50;
}

CGFloat widthCallback(void* refCon) {
    return 100;
}

@interface ViewController ()
@property (nonatomic, strong) QQAttributedLabel *label;
@property (nonatomic, strong) NSMutableAttributedString *attributedText;
@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];

    _attributedText = [[NSMutableAttributedString alloc] initWithString:@"I added kCTRunDelegateAttributeName to the attributedText, but it does not work"];

    CTRunDelegateCallbacks callbacks;
    memset(&callbacks, 0, sizeof(CTRunDelegateCallbacks));
    callbacks.version = kCTRunDelegateVersion1;
    callbacks.getAscent = ascentCallback;
    callbacks.getDescent = descentCallback;
    callbacks.getWidth = widthCallback;
    CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, NULL);
    if (NULL != delegate) {
        unichar objectReplacementChar = 0xFFFC;
        NSString *objectReplacementString = [NSString stringWithCharacters:&objectReplacementChar length:1];
        NSMutableAttributedString* space = [[NSMutableAttributedString alloc] initWithString:objectReplacementString];

        CFRange range = CFRangeMake(0, 1);
        CFMutableAttributedStringRef spaceString = (__bridge_retained CFMutableAttributedStringRef)space;
        CFAttributedStringSetAttribute(spaceString, range, kCTRunDelegateAttributeName, delegate);
        CFAttributedStringSetAttribute(spaceString,
                                       range,
                                       kCTWritingDirectionAttributeName,
                                       (__bridge CFArrayRef)@[@(kCTWritingDirectionLeftToRight)]);
        CFRelease(delegate);
        CFRelease(spaceString);

        [_attributedText insertAttributedString:space atIndex:30];
    }

    _label = [[QQAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
    _label.backgroundColor = [UIColor yellowColor];
    _label.numberOfLines = 0;
    _label.attributedText = _attributedText;
    [_label sizeToFit];
    _label.center = self.view.center;
    [self.view addSubview:_label];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
阅读 5.6k
1 个回答

通过apple-touch-startup-image设置了启动

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏