ios 像微博那样,怎么在label内文字"@"处给label添加点击事件件和文字变色

48961人阅读
转载分享(33)
记录(42)
UIEdgeInsets
typedef struct UIEdgeInsets { &
& & CGFloat top, left, bottom, &// specify amount to inset (positive) for each of the edges. values can be negative to 'outset' &
} UIEdgeI &
在UIButton中有三个对EdgeInsets的设置:ContentEdgeInsets、titleEdgeInsets、imageEdgeInsets
@property(nonatomic) & & & & &UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero &
@property(nonatomic) & & & & &UIEdgeInsets titleEdgeI & & & & & & & &// default is UIEdgeInsetsZero &
@property(nonatomic) & & & & &BOOL & & & & reversesTitleShadowWhenH // default is NO. if YES, shadow reverses to shift between engrave and emboss appearance &
@property(nonatomic) & & & & &UIEdgeInsets imageEdgeI & & & & & & & &// default is UIEdgeInsetsZero &
UIEdgeInsetsMake
里面的四个参数表示距离上边界、左边界、下边界、右边界的距离,默认都为零,title/image在button的正中央
UIKIT_STATIC_INLINE UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) { &
& & UIEdgeInsets insets = {top, left, bottom, right}; &
&self.view.backgroundColor = [UIColor blackColor]; &
& & UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//button的类型 &
& & button.frame = CGRectMake(100, 100,90, 90);//button的frame &
& &button.backgroundColor = [UIColor cyanColor];//button的背景颜色 &
// & &[button setBackgroundImage:[UIImage imageNamed:@&man_64.png&] forState:UIControlStateNormal]; &
// & &在UIButton中有三个对EdgeInsets的设置:ContentEdgeInsets、titleEdgeInsets、imageEdgeInsets &
& & [button setImage:[UIImage imageNamed:@&IconHome@2x.png&] forState:UIControlStateNormal];//给button添加image &
& & button.imageEdgeInsets = UIEdgeInsetsMake(5,13,21,button.titleLabel.bounds.size.width);//设置image在button上的位置(上top,左left,下bottom,右right)这里可以写负值,对上写-5,那么image就象上移动5个像素 &
& & [button setTitle:@&首页& forState:UIControlStateNormal];//设置button的title &
& & button.titleLabel.font = [UIFont systemFontOfSize:16];//title字体大小 &
& & button.titleLabel.textAlignment = NSTextAlignmentC//设置title的字体居中 &
& & [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//设置title在一般情况下为白色字体 &
& & [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];//设置title在button被选中情况下为灰色字体 &
& & &button.titleEdgeInsets = UIEdgeInsetsMake(71, -button.titleLabel.bounds.size.width-50, 0, 0);//设置title在button上的位置(上top,左left,下bottom,右right) &
// & &[button setContentEdgeInsets:UIEdgeInsetsMake(70, 0, 0, 0)];// &
// & button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentC//设置button的内容横向居中。。设置content是title和image一起变化 &
& & [button addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside]; &
& & [self.view addSubview:button]; &
//button相应的事件 &
-(void)tap { &
& & NSLog(@&tap a button&); &
& & UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@&hello& message:@&willingseal& delegate:self cancelButtonTitle:@&ok& otherButtonTitles: nil nil]; &
& & [alertView show]; &
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:174838次
积分:1723
积分:1723
排名:第15113名
原创:31篇
转载:35篇
评论:19条
(1)(1)(1)(2)(4)(1)(4)(4)(4)(8)(2)(6)(9)(6)(6)(8)uilabel 的相关处理
类富文本(自动换行,设置行高,同一个label多种颜色,给文字加下划线 首行缩进 等 等)
uilabel 的相关处理
类富文本(自动换行,设置行高,同一个label多种颜色,给文字加下划线 首行缩进 等 等)
[摘要:1.主动换止 UIFont *font=[UIFont systemFontOfSize:lableFont]; self.numberOfLines=0; self.lineBreakMode=NSLineBreakByWordW self.text= self.font= self.textColor= CGSize sizeW=[content sizeWithFont:font constraine]
1.自动换行
UIFont *font=[UIFont systemFontOfSize:lableFont];
self.numberOfLines=0;
self.lineBreakMode=NSLineBreakByWordW
self.text=
self.font=
self.textColor=
CGSize sizeW=[content sizeWithFont:font constrainedToSize:lableSize lineBreakMode:NSLineBreakByWordWrapping];
self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y, sizeW.width, sizeW.height);// 此处的 siezeW.with &可以根据需要 设置为 你想要的宽度 如在cell 中让他为300 或者200 等等 此处设置为siezeW.with 是为了 让label 适应宽度 & 同样的在cell 中 为了让cell 自适应高度 只要将 cell的高度设置为 sizeW.height &就可以了
有时候根据工程需求 需要根据比如“冒号” “ 分号”等换行 就可以使用
// &根据 冒号 分号等的分行 显示 & rn即为分行 把想换行的特殊符号 换为rn 就可以了然后在加上上面的就可以根据你字符串的内容自动换行了
NSString *str = [str1 stringByReplacingOccurrencesOfString:@&;& withString:@&rn&];
2.调整uilabel 行高间距
UILabel *contenLable=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, 300, 310)];
contenLable.textColor=[Utils getDarkColor];
contenLable.font=[UIFont systemFontOfSize:13];
contenLable.lineBreakMode=UILineBreakModeWordW
contenLable.numberOfLines=0;
// 设置字体间每行的间距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineHeightMultiple = 15.0f;
paragraphStyle.maximumLineHeight = 15.0f;
paragraphStyle.minimumLineHeight = 15.0f;
paragraphStyle.lineSpacing = 4.0f;// 行间距
NSString *string = @&此处的 siezeW.with &可以根据需要 设置为 你想要的宽度 如在cell 中让他为300 或者200 等等 此处设置为siezeW.with 是为了 让label 适应宽度 & 同样的在cell 中 为了让cell 自适应高度 只要将 cell的高度设置为 sizeW.height &就可以了&;
NSDictionary *ats = @{
NSParagraphStyleAttributeName : paragraphStyle,
contenLable.attributedText = [[NSAttributedString alloc] initWithString:string attributes:ats];
[view addsubview: contenLabel];
3.对label 添加下划线同一个label 多种颜色&
NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:str];
NSRange contentRange = { 0,[content length]};
[content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
&添加 颜色 &只有在ios 7上可以
// NSRange contentRangenew = {0,2};
// [content addAttribute:NSForegroundColorAttributeName value:(id)[[UIColor blueColor]CGColor] range:contentRangenew];
// NSRange conw = {4,19};
// [content addAttribute:NSForegroundColorAttributeName value:(id)[[UIColor blackColor]CGColor] range:conw];
Label.attributedText =
4.总结以上
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentJ//设置对齐方式
paragraph.lineBreakMode = NSLineBreakByWordW
NSAttributedString的初始化方法有
& &-initWithString:用String初始化,并没有Attributed信息。
& &-initWithAttributedString:用AttributedString去初始化。
& -initWithString:Attributed:用string及attribute的dictionary来初始化。
具体AttributtedString属性的键值对如下:
& &NSString *const NSFontAttributeN//值为UIFont,设置字体,默认值为12-point Helvetica(Neue) 。
NSString *const NSParagraphStyleAttributeN//值为NSParagraphStyle,设置段落属性,默认值为[NSParagraphStyle defaultParagraphStyle]返回的值。
NSMutableParagraphStyle与NSParagraphStyle包括一下属性
alignment //对齐方式
firstLineHeadIndent //首行缩进
headIndent //缩进
tailIndent //尾部缩进
lineBreakMode //断行方式
maximumLineHeight //最大行高
minimumLineHeight //最低行高
lineSpacing //行距
paragraphSpacing //段距
paragraphSpacingBefore //段首空间
baseWritingDirection //句子方向
lineHeightMultiple //可变行高,乘因数。
hyphenationFactor //连字符属性
NSString *const NSForegroundColorAttributeN//值为UIColor,字体颜色 默认黑色
NSString *const NSBackgroundColorAttributeN//值为UIColor,字体背景色,默认没有。
NSString *const NSLigatureAttributeN//值为整型NSNumber,连字属性,一般中文用不到,在英文中可能出现相邻字母连笔的情况。0为不连笔;1为默认连笔,也是默认值;2在ios 上不支持。
NSString *const NSKernAttributeN//值为浮点数NSNumber,字距属性,默认值为0。
NSString *const NSStrikethroughStyleAttributeN//值为整型NSNumber,可取值为
NSUnderlineStyleNone = 0×00,
NSUnderlineStyleSingle = 0×01,
};设置删除线。
NSString *const NSUnderlineStyleAttributeN//同上。设置下划线。
NSString *const NSStrokeColorAttributeN//值为UIColor,默认值为nil,设置的属性同ForegroundColor。
NSString *const NSStrokeWidthAttributeN//值为浮点数NSNumber。设置比画的粗细。
NSString *const NSShadowAttributeN//值为NSShadow,设置比画的阴影,默认值为nil。
NSString *const NSVerticalGlyphFormAttributeN//值为整型NSNumber,0为水平排版的字,1为垂直排版的字。
首行缩进举例子
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];// 下面就可以设置他的各种属性了
paragraph.firstLineHeadIndent = 20;// 这里 &只是设置了首行缩进 &我这里设置首行缩进 20 缩进两个字 &根据需求可以调节缩进的大小
NSDictionary *ats = @{
NSParagraphStyleAttributeName : paragraph,
Label.attributedText = [[NSMutableAttributedString alloc]initWithString:str attributes:ats];
感谢关注 Ithao123iOS频道,是专门为互联网人打造的学习交流平台,全面满足互联网人工作与学习需求,更多互联网资讯尽在 IThao123!
Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。
Hadoop是一个由Apache基金会所开发的分布式系统基础架构。
用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力进行高速运算和存储。
Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上;而且它提供高吞吐量(high throughput)来访问应用程序的数据,适合那些有着超大数据集(large data set)的应用程序。HDFS放宽了(relax)POSIX的要求,可以以流的形式访问(streaming access)文件系统中的数据。
Hadoop的框架最核心的设计就是:HDFS和MapReduce。HDFS为海量的数据提供了存储,则MapReduce为海量的数据提供了计算。
产品设计是互联网产品经理的核心能力,一个好的产品经理一定在产品设计方面有扎实的功底,本专题将从互联网产品设计的几个方面谈谈产品设计
随着国内互联网的发展,产品经理岗位需求大幅增加,在国内,从事产品工作的大部分岗位为产品经理,其实现实中,很多从事产品工作的岗位是不能称为产品经理,主要原因是对产品经理的职责不明确,那产品经理的职责有哪些,本专题将详细介绍产品经理的主要职责
IThao123周刊下次自动登录
关注移动互联网和移动APP开发工具、开发框架、测试工具、微信开发、Android源码、Android开源类库以及各种开源组件的IT科技网站
现在的位置:
iOS开发——UITabbar详解(仿新浪微博的Demo)
本文主要讲述了UITabbar的概念及用法,并且使用UITabbar写了一个模仿新浪微博的demo,现在分享给iOS程序员兄弟们,希望给他们的开发工作带来帮助。
前言:UITabBarController和UINavigationController是大多数App选择的导航模式。而UITabBarController的UITabBar又是其呈现给用户的UI部分,所以了解其属性,以及如何自定义非常重要。本文模仿新浪微博写一个简单的Tabbar,感兴趣的同学可以来看看,Demo的实现我会在本文最后简单提一下。
Demo 效果(图标文字随便写的)
Demo的具体代码就不讲解了,感兴趣的同学下载下来看看就知道怎么回事,而且,我的资源都是不要积分就可以下的。
http://download.csdn.net/detail/hello_hwc/9068705
UITabbar的常用属性
tintColor//定义TabBarItems的选种颜色
例如,设为红色
几个Image相关的属性
backgroundImage
设置UITabbar的背景图片,例如,设置上文最开始的Demo的背景图片为渐变
shadowImage //阴影图片
selectionIndicatorImage //选种后再tabbar之上,再baritem之下的图片。
translucent //是否透明
UIBarItem的几个常用属性
image/selectedImage //图片,和选中后的图片
例如,我的Demo中,对中间的Item进行了如下设置
保证怎么点击都使用我想要的图片
item.image = [midImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item.selectedImage = [midImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
调整title和Icon的位置
titlePositionAdjustment/imageInsets
例如,我的Demo中,中间的是大图,没有title的,要调整Icon的位置
item.imageInsets = UIEdgeInsetsMake(5.0, 0, -5.0, 0);
设置字体样式&"/kf/ware/vc/" target="_blank" class="keylink"&vcD4NCjxwcmUgY2xhc3M9"brush:"&
setTitleTextAttributes:forState:
例如,使用如下代码把字体设为蓝色
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blueColor]} forState:UIControlStateNormal];
设置前后对比
Demo的讲解
模糊使用UIVisualEffectView 动画我尝试了Keyframe,UISnapBehaviour,以及Demo中的代码,发现还是Demo中的更加清楚一点。 使用tag来传递第几个icon被点击
【上篇】【下篇】iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
UIEdgeInsets
typedef struct UIEdgeInsets { &
& & CGFloat top, left, bottom, &// specify amount to inset (positive) for each of the edges. values can be negative to 'outset' &
} UIEdgeI &
在UIButton中有三个对EdgeInsets的设置:ContentEdgeInsets、titleEdgeInsets、imageEdgeInsets
@property(nonatomic) & & & & &UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero &
@property(nonatomic) & & & & &UIEdgeInsets titleEdgeI & & & & & & & &// default is UIEdgeInsetsZero &
@property(nonatomic) & & & & &BOOL & & & & reversesTitleShadowWhenH // default is NO. if YES, shadow reverses to shift between engrave and emboss appearance &
@property(nonatomic) & & & & &UIEdgeInsets imageEdgeI & & & & & & & &// default is UIEdgeInsetsZero &
UIEdgeInsetsMake
里面的四个参数表示距离上边界、左边界、下边界、右边界的距离,默认都为零,title/image在button的正中央
UIKIT_STATIC_INLINE UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) { &
& & UIEdgeInsets insets = {top, left, bottom, right}; &
&self.view.backgroundColor = [UIColor blackColor]; &
& & UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//button的类型 &
& & button.frame = CGRectMake(100, 100,90, 90);//button的frame &
& &button.backgroundColor = [UIColor cyanColor];//button的背景颜色 &
// & &[button setBackgroundImage:[UIImage imageNamed:@&man_64.png&] forState:UIControlStateNormal]; &
// & &在UIButton中有三个对EdgeInsets的设置:ContentEdgeInsets、titleEdgeInsets、imageEdgeInsets &
& & [button setImage:[UIImage imageNamed:@&IconHome@2x.png&] forState:UIControlStateNormal];//给button添加image &
& & button.imageEdgeInsets = UIEdgeInsetsMake(5,13,21,button.titleLabel.bounds.size.width);//设置image在button上的位置(上top,左left,下bottom,右right)这里可以写负值,对上写-5,那么image就象上移动5个像素 &
& & [button setTitle:@&首页& forState:UIControlStateNormal];//设置button的title &
& & button.titleLabel.font = [UIFont systemFontOfSize:16];//title字体大小 &
& & button.titleLabel.textAlignment = NSTextAlignmentC//设置title的字体居中 &
& & [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//设置title在一般情况下为白色字体 &
& & [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];//设置title在button被选中情况下为灰色字体 &
& & &button.titleEdgeInsets = UIEdgeInsetsMake(71, -button.titleLabel.bounds.size.width-50, 0, 0);//设置title在button上的位置(上top,左left,下bottom,右right) &
// & &[button setContentEdgeInsets:UIEdgeInsetsMake(70, 0, 0, 0)];// &
// & button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentC//设置button的内容横向居中。。设置content是title和image一起变化 &
& & [button addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside]; &
& & [self.view addSubview:button]; &
//button相应的事件 &
-(void)tap { &
& & NSLog(@&tap a button&); &
& & UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@&hello& message:@&willingseal& delegate:self cancelButtonTitle:@&ok& otherButtonTitles: nil nil]; &
& & [alertView show]; &
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'> 像微博那样,怎么在label内文字“@. ”处添加点击事件和文字变色
像微博那样,怎么在label内文字“@. ”处添加点击事件和文字变色
xiaogougou & &
发布时间: & &
浏览:6 & &
回复:1 & &
悬赏:0.0希赛币
像微博那样,如何在label内文字“@... ”处添加点击事件和文字变色问过别人&如何实现像微博列表那样&在“@...&”处文字变色并添加点击事件
他说在“@...&”处&添加&uibutton
可是我怎么知道&“@...&”&的具体位置呢?
请问&用uibutton&替换&“@...&”&的思路是否正确?
具体该怎么实现?
方案1:协议里告诉哪段文字可以作为链接处理
方案2:自己在文字里搜索到@,@后面的都当做链接
1更靠谱xiaoguan1 & &
& & (0)(0)
本问题标题:
本问题地址:
温馨提示:本问题已经关闭,不能解答。
暂无合适的专家
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&}

我要回帖

更多关于 ios label点击事件 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信