iOS晋级技术文章,请关注 hehuoya.com 合伙呀

默认是底部对齐,其实对的也不齐,

目标效果: 

代码:

NSBaselineOffsetAttributeName

基线偏移量: 
调整: NSBaselineOffsetAttributeName的值得大小,就可以得到不同的对齐位置

CGFloat fontRatio = 0.16;//基线偏移比率
  • 1

CGFloat fontRatio = 0.66;//基线偏移比率
  • 1

- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"富文本"; NSString *text = @"(2) 3 : 2 (1)"; NSInteger fontSize1 = 30;
NSInteger fontSize2 = 16;
CGFloat fontRatio = 0.66;//基线偏移比率 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(150, 200, 150, 40)];
label.text = text; NSMutableAttributedString *attributedStringM = [[NSMutableAttributedString alloc] initWithString:text]; [attributedStringM addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize2] range:NSMakeRange(0, 3)];
[attributedStringM addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)]; [attributedStringM addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize1] range:NSMakeRange(3, text.length - 6)];
[attributedStringM addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(3, text.length - 6)]; [attributedStringM addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize2] range:NSMakeRange(text.length - 3, 3)];
[attributedStringM addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(text.length - 3, 3)]; //不同大小的文字水平中部对齐(默认是底部对齐)
[attributedStringM addAttribute:NSBaselineOffsetAttributeName value:@(fontRatio * (fontSize1 - fontSize2)) range:NSMakeRange(0, 3)];
[attributedStringM addAttribute:NSBaselineOffsetAttributeName value:@(fontRatio * (fontSize1 - fontSize2)) range:NSMakeRange(text.length - 3, 3)]; label.attributedText = attributedStringM;
[self.view addSubview:label];
}

(转)解决NSMutableAttributedString富文本,不同文字大小水平轴对齐问题(默认底部对齐)的更多相关文章

  1. Axure 文本框去掉边框 富文本 粘贴文字图标

    在今天做原型的过程中,碰到两个问题: 1 文本框该如何去掉边框 2 富文本粘贴文字图标 第一个问题:首先是思路错了,又跑到元件上面找边框,跑到style里面去border的线,结果是不成功. 正解:属 ...

  2. iOS - 富文本直接设置文字的字体大小和颜色

    富文本效果图: 富文本实现代码: UILabel *orderSureLabel = [Common lableFrame:CGRectZero title:] textColor:[UIColor ...

  3. iOS - NSMutableAttributedString富文本的实现

    NSMutableAttributedString继承于NSAttributedString(带属性的字符串)能够简单快速实现富文本的效果;不多说直接上效果图和代码,通俗易懂: (一)效果图: (二) ...

  4. NSMutableAttributedString 富文本的使用

    //富文本的使用 UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; testLabel.backgroun ...

  5. NSMutableAttributedString(富文本)的简单使用

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  6. NSMutableAttributedString 富文本删除线的用法

    #import <UIKit/UIKit.h> //价格 NSString *priceStr = @"99元 剁手价66元"; NSMutableAttributed ...

  7. 富文本 文字图片点击,(TextView)

    textview上的富文本支持 文字,图片的点击事件 - (void)protocolIsSelect:(BOOL)select { NSMutableAttributedString *attrib ...

  8. 【代码笔记】iOS-获得富文本设置以后的文字高度

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  9. (转)淘淘商城系列——KindEditor富文本编辑器的使用

    http://blog.csdn.net/yerenyuan_pku/article/details/72809794 通过上文的学习,我们知道了怎样解决KindEditor富文本编辑器上传图片时的浏 ...

随机推荐

  1. 简易OA漫谈之工作流设计(五,直接上级)

    规则引擎里比较复杂的问题就是:配置步骤的审批人. 某一个步骤由谁来审批,有很多复杂情况: 1.指定某一个具体的人.这种通常用于一些特殊的岗位,全公司只有一个,比如小公司里的财务,人事专员等. 2.指定 ...

  2. angular6 input节流

    一直以为   pipe(debounceTime(1000), distinctUntilChanged())  不起作用 原因:使用方法错误 <input type="text&qu ...

  3. 苹果手机的SB系列(3)超级烦人的账户解锁?

    不知道大家有没有这种体验,Iphone 每隔一段时间后账户就被锁定了,也不告诉你原因,就是要解锁? 我怎么感觉比做的比支付宝差多了,我注册支付宝十几年,也没有动不动就告诉了有安全原因,要解锁,要重置密 ...

  4. Android NDK pthreads详细使用

    这个pthread.h文件可以在NDK环境里创建子线程,并对线程能够做出互斥所.等待.销毁等控制. 写这个博客的原因是我要写如何使用FFmpeg播放视频,因为同时需要播放音频和视频所以需要开启线程,并 ...

  5. 版本管理工具小乌龟TortoiseGit的安装和使用(1)

    1.软件的安装:1.1 安装 Git使用软件管理工具搜索 Git:

  6. springboot秒杀课程学习整理1-6

    1)活动模型设计 配饰秒杀的模型(promoModel)id promoName startDate(建议使用joda-time) endDate itemId promoItemPrice 数据库( ...

  7. React-Error

    1. react创建一个单页应用,官网给的命令是ngx create-react-app my-project 2. window上表示,安装失败 3.解放办法:npm install -g crea ...

  8. Spark 中Java实现数据库Row转Rating

    Dataset<Row> ratings = mlsc.sql("SELECT user,movie,rating FROM data");JavaRDD<Row ...

  9. http协议与https协议的前世今生

    一.Http与Https的区别: HTTP 的URL 以http:// 开头,而HTTPS 的URL 以https:// 开头 HTTP 是不安全的,而 HTTPS 是安全的 HTTP 标准端口是80 ...

  10. SpringBoot配置Swagger实例(POST接收json参数)

    工程目录结构: 首先,引入jar包,只需要以下两个即可 <dependency> <groupId>io.springfox</groupId> <artif ...