ios实现编辑文本添加#话题、@好友

ios实现编辑文本添加#话题、@好友,第1张

效果如图所示:

在项目中导入YYKit和Masonry

1.引用刚才导入的Masonry和YYKit中的YYTextView、YYText。BLPublishBeauView为自定义的文本编辑类

2.声明tfView,遵循YYTextViewDelegate,文本内容变动时会触发代理方法

3.实现编辑文本时添加话题、好友

3.1 监听话题和@好友的点击事件(didClickLink)

3.2 监听文本内容变动(textViewDidChange)

源码WPAttributedMarkup, WPAttributedMarkup能给Label中某关键字添加文字效果或点击事件。Label中的某个关键字词可以改变字体的特性如颜色、加粗,下划线等,也可以为某个关键字词添加点击事件。

<ignore_js_op>

使用方法:

通过创建不同的style的字典后就可以对label的属性进行设置,示例如下:

// Example using fonts and colours

NSDictionary* style1 = @{@"body":[UIFont fontWithName:@"HelveticaNeue" size:18.0],

@"bold":[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0],

@"red": [UIColor redColor]}

// Example using arrays of styles, dictionary attributes for underlining and image styles

NSDictionary* style2 = @{@"body" :

@[[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0],

[UIColor darkGrayColor]],

@"u": @[[UIColor blueColor],

@{NSUnderlineStyleAttributeName : @(kCTUnderlineStyleSingle|kCTUnderlinePatternSolid)}

],

@"thumb":[UIImage imageNamed:@"thumbIcon"] }

// Example using blocks for actions when text is tapped. Uses the 'link' attribute to style the links

NSDictionary* style3 = @{@"body":[UIFont fontWithName:@"HelveticaNeue" size:22.0],

@"help":[WPAttributedStyleAction styledActionWithAction:^{

NSLog(@"Help action")

}],

@"settings":[WPAttributedStyleAction styledActionWithAction:^{

NSLog(@"Settings action")

}],

@"link": [UIColor orangeColor]}

self.label1.attributedText = [@"AttributedBoldRedtext" attributedStringWithStyleBook:style1]

self.label2.attributedText = [@"[td]Multiplestylestext[td]" attributedStringWithStyleBook:style2]

<br/>

本文来源于<b>大学IT网</b>

 


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

原文地址:https://54852.com/bake/11374504.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存