iOS - UILabel添加图片之富文本的简单应用
2023-09-11 14:21:34 时间
//创建富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" -积分商城-热门商品-优惠券,后续会陆续打通东方明珠塔等各类上海文化消费优惠券。"]; //NSTextAttachment可以将要插入的图片作为特殊字符处理 NSTextAttachment *attch = [[NSTextAttachment alloc] init]; //定义图片内容及位置和大小 attch.image = [UIImage imageNamed:@"home_profile_icon"]; attch.bounds = CGRectMake(0, 0, 15, 15); //创建带有图片的富文本 NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch]; //将图片放在最后一位 //[attri appendAttributedString:string]; //将图片放在第一位 [attri insertAttributedString:string atIndex:0]; //用label的attributedText属性来使用富文本 _imgLabel.attributedText = attri; //若想对图片添加点击事件,现在的想法是在label上添加一个透明按钮,位置大小跟图片的相同 _imgLabel.userInteractionEnabled = YES; UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom]; clearBtn.frame = CGRectMake(0, 12, attch.bounds.size.width, attch.bounds.size.height+3); clearBtn.backgroundColor = [UIColor clearColor]; [clearBtn addTarget:self action:@selector(alertSth) forControlEvents:UIControlEventTouchUpInside]; [_imgLabel addSubview:clearBtn];
相关文章
- Flex AIR应用拍照功能(Android和IOS版本)
- (NO.00005)iOS实现炸弹人游戏(三):从主场景类谈起
- [ IOS ] iOS-控制器View的创建和生命周期
- IOS 30多个iOS常用动画,带详细注释
- IOS应用版本更新(自动提醒用户)
- iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist)
- iOS中 蓝牙2.0详解/ios蓝牙设备详解
- IOS NSInvocation应用与理解
- IOS调用系统通讯录
- 《iPhone与iPad开发实战—iOS经典应用剖析》连载六
- 《iPhone与iPad开发实战—iOS经典应用剖析》连载四
- EasyPlayer iOS开源流媒体播放器中AAC解码PCM问题
- ios swift UITabBarController present dismiss
- iOS(swift) UILabel根据里面的文字自适应,自动调整宽高
- iOS @2x @3x图的区别和理解
- ios使用openUrl进行应用跳转
- IOS_OC_id ,NSObject, id<NSObject>差别
- iOS让软键盘消失的简单方法