我正在修改我找到的课程。它正在强调超链接。我希望它也可以加粗超链接。我不知道如何使用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];
}
答案 0 :(得分:1)
您需要将字体设置为“粗体”字体(例如“Helvetica”和“Helvetica-Bold”)。
addAttribute:value:range:
方法,为第一个参数传递“kCTFontAttributeName”,为第二个参数传递CTFont对象。CFRelease(CFType obj)
。