ios – UITextView中链接的不同样式

ios – UITextView中链接的不同样式,第1张

概述我希望我的UITextView包括纯文本,链接,电话号码和其他UIDataDetectorTypes.例如,纯文本 – 黑色,链接 – 带下划线的蓝色,主题标签 – 绿色.所以,我解析文本并添加到主题标签的链接: [attributedString addAttribute:NSLinkAttributeName value:[NSString stringWithFormat:@"hashtag 我希望我的UITextVIEw包括纯文本,链接,电话号码和其他UIDataDetectorTypes.例如,纯文本 – 黑色,链接 – 带下划线的蓝色,主题标签 – 绿色.所以,我解析文本并添加到主题标签的链接:

[attributedString addAttribute:NSlinkAttributename value:[Nsstring stringWithFormat:@"hashtag://%@",hashtag] range:range];

如果dataDetectorTypes设置为UIDataDetectorTypeAll,则默认情况下会检测其他链接.但是如何更改主题标签的链接样式

解决方法 您可以使用官方Twitter库跟踪名为 twitter text的主题标签以及 TTTAttributedLabel.

HashTags有特殊条件,这些条件可能超过前面带#的字母数字字符.

twitter文本库将自动为您找到主题标签的范围,然后您应该能够使用TTTAttributedLabel对其进行格式化.

以下是两个库的示例实现.

- (voID)scanForHashtag:(NSArray *)hashTags fromLabel:(ID)sender{    TTTAttributedLabel *label = (TTTAttributedLabel *)sender;    for (TwitterTextEntity *entity in hashTags) {        UIFont *boldSystemFont = [UIFont FontWithname:@"Helvetica-Bold" size:12];        CTFontRef Font = CTFontCreateWithname((__brIDge CFStringRef)boldSystemFont.Fontname,boldSystemFont.pointSize,NulL);        NSArray *keys = [[NSArray alloc] initWithObjects:(ID)kCTForegroundcolorAttributename,(ID)kCTUnderlinestyleAttributename,(Nsstring *)kCTFontAttributename,nil];        UIcolor *hashtagcolor;        hashtagcolor = [UIcolor greencolor];        NSArray *objects = [[NSArray alloc] initWithObjects:hashtagcolor,[NSNumber numberWithInt:kCTUnderlinestyleNone],(__brIDge ID)Font,nil];        NSDictionary *linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];        label.linkAttributes = linkAttributes;        Nsstring *hashtag = [label.text substringWithRange:entity.range];        if (entity.type == 2) { //Hashtag            [label addlinktophoneNumber:hashtag withRange:entity.range];        } else if (entity.type == 0) { //URL            NSURL *url = [NSURL URLWithString:[label.text substringWithRange:entity.range]];            [label addlinkToURL:url withRange:entity.range];        }    }}- (voID)attributedLabel:(TTTAttributedLabel *)label dIDSelectlinkWithPhoneNumber:(Nsstring *)phoneNumber{    //Do whatever you need when the hashtag is tapped.}[self scanForHashtag:[TwitterText hashTagsInText:@"The text that contains the tweet with hashTags." checkingURloverlap:NO] fromLabel:yourLabel]; //yourLabel is the label that contains the text so it would be formatted.

编辑UITextVIEw

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];[attributedString addAttribute:NSBackgroundcolorAttributename                   value:[UIcolor yellowcolor]                   range:entity.range];
总结

以上是内存溢出为你收集整理的ios – UITextView中链接的不同样式全部内容,希望文章能够帮你解决ios – UITextView中链接的不同样式所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1044959.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存