如何使用 CoreText 将文本加粗?
我正在修改我找到的一个类。它在超链接下划线。我希望它也将超链接加粗。我不知道如何使用 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];
}
I am modifying a class that I found. It is underlining hyperlinks. I would like it to bold the hyperlink as well. I am not sure how to do this with 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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将字体设置为“粗体”字体(例如“Helvetica”和“Helvetica-Bold”)。
You need to set the font to a "Bold" font (e.g. "Helvetica" and "Helvetica-Bold").
addAttribute:value:range:
method of your NSAttributedString, pass "kCTFontAttributeName" for the first parameter and your CTFont object for the second.CFRelease(CFType obj)
.