Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何将CoreText转换为粗体文本?_Iphone_Objective C_Cocoa Touch_Nsstring_Core Text - Fatal编程技术网

Iphone 如何将CoreText转换为粗体文本?

Iphone 如何将CoreText转换为粗体文本?,iphone,objective-c,cocoa-touch,nsstring,core-text,Iphone,Objective C,Cocoa Touch,Nsstring,Core Text,我正在修改找到的一个类。它在超链接下面加下划线。我想它加粗的超链接以及。我不知道如何使用CoreText实现这一点 -(NSMutableAttributedString*)attributedTextWithLinks { NSMutableAttributedString* str = [self.attributedText mutableCopy]; if (!str) return nil; if (self.automaticallyDetectLinks)

我正在修改找到的一个类。它在超链接下面加下划线。我想它加粗的超链接以及。我不知道如何使用CoreText实现这一点

-(NSMutableAttributedString*)attributedTextWithLinks {
    NSMutableAttributedString* str = [self.attributedText mutableCopy];
    if (!str) return nil;

    if (self.automaticallyDetectLinks) {
        NSError* error = nil;
        NSDataDetector* linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
        [linkDetector enumerateMatchesInString:[str string] options:0 range:NSMakeRange(0,[[str string] length])
                                    usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
         {
             int32_t uStyle = self.underlineLinks ? kCTUnderlineStyleSingle : kCTUnderlineStyleNone;
             UIColor* thisLinkColor = (delegate && [delegate respondsToSelector:@selector(colorForLink:underlineStyle:)])
             ? [delegate colorForLink:result underlineStyle:&uStyle] : self.linkColor;

             if (thisLinkColor)
                 [str setTextColor:thisLinkColor range:[result range]];
             if (uStyle>0)
                 [str setTextUnderlineStyle:uStyle range:[result range]];
         }];
    }
    [customLinks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
     {
         NSTextCheckingResult* result = (NSTextCheckingResult*)obj;

         int32_t uStyle = self.underlineLinks ? kCTUnderlineStyleSingle : kCTUnderlineStyleNone;
         UIColor* thisLinkColor = (delegate && [delegate respondsToSelector:@selector(colorForLink:underlineStyle:)])
         ? [delegate colorForLink:result underlineStyle:&uStyle] : self.linkColor;

         if (thisLinkColor)
             [str setTextColor:thisLinkColor range:[result range]];
         if (uStyle>0)
             [str setTextUnderlineStyle:uStyle range:[result range]];
     }];
    return [str autorelease];
}

您需要将字体设置为“粗体”字体(例如“Helvetica”和“Helvetica粗体”)

  • 使用您选择的粗体字体创建CTFont对象
  • 使用NSAttribute字符串的
    addAttribute:value:range:
    方法,第一个参数传递“kCTFontAttributeName”,第二个参数传递CTFont对象
  • 为避免泄漏,请记住,如果CTFont对象是由名称中包含“create”的方法创建的,请释放该对象;使用
    CFRelease(CFType obj)

  • 您需要将字体设置为“粗体”字体(例如“Helvetica”和“Helvetica粗体”)

  • 使用您选择的粗体字体创建CTFont对象
  • 使用NSAttribute字符串的
    addAttribute:value:range:
    方法,第一个参数传递“kCTFontAttributeName”,第二个参数传递CTFont对象
  • 为避免泄漏,请记住,如果CTFont对象是由名称中包含“create”的方法创建的,请释放该对象;使用
    CFRelease(CFType obj)