1、看效果

2、介绍用途

例如:下面的设计要求8%的字和返利的框对齐。如图:

碰到这种情况。单纯的uilabel 是无能为力的。有的朋友还不信。可以调整位子来对其啊,但是,你换一个手机他就会又变的。所以,你就跟着我来吧 !本例提供两种方法,其中一种有使用限制。

3、方法一(无限限制的)。

//

//  UILabelView.m

//  UILabel 的文字对齐Demo

//

//  Created by MAC on 16/10/12.

//  Copyright © 2016年 NetworkCode小贱. All rights reserved.

//

#import "UILabelView.h"

@implementation UILabelView

-(instancetype)initWithFrame:(CGRect)frame{

if (self==[super initWithFrame:frame]) {

self.backgroundColor = [UIColor redColor ];

}

return self;

}

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

/* 获取上下文*/

CGContextRef contentRef = UIGraphicsGetCurrentContext();

/* 监控对象是否为空*/

NSCAssert(self.text!=nil, @"绘制内容不能为空");

/* 获取文本的宽与高*/

CGFloat SizeHeight = self.bounds.size.height;

CGFloat SizeWeight = self.bounds.size.width;

/* 计算文本的大小*/

CGSize TextSize = [self.text boundingRectWithSize:self.bounds.size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:self.attrs context:nil].size;

CGRect drawRect  = CGRectZero;

if (_styple == TextStyleTop || _styple == 0) {

drawRect= CGRectMake((SizeWeight-TextSize.width)*0.5, 0, TextSize.width, TextSize.height);

}else if(self.styple == TextStyleRight){

drawRect= CGRectMake(SizeWeight - TextSize.width, (SizeHeight-TextSize.height)*0.5, TextSize.width, TextSize.height);

}else if(self.styple == TextStyleBottom){

drawRect= CGRectMake((SizeWeight-TextSize.width)*0.5, SizeHeight-TextSize.height, TextSize.width, TextSize.height);

}else if(self.styple == TextStyleLeft){

drawRect= CGRectMake(0, (SizeHeight-TextSize.height)*0.5, TextSize.width, TextSize.height);

}else if(self.styple == TextStyleCenter){

drawRect= CGRectMake((SizeWeight-TextSize.width)*0.5, (SizeHeight-TextSize.height)*0.5, TextSize.width, TextSize.height);

}else if(self.styple == TextStyleLeftTop){

drawRect= CGRectMake(0, 0, TextSize.width, TextSize.height);

}else if(self.styple == TextStyleRightTop){

drawRect= CGRectMake(SizeWeight-TextSize.width, 0, TextSize.width, TextSize.height);

}else if(self.styple == TextStyleLeftBottom){

drawRect= CGRectMake(0, SizeHeight-TextSize.height, TextSize.width, TextSize.height);

}else if(self.styple == TextStyleRightBottom){

drawRect= CGRectMake(SizeWeight-TextSize.width, SizeHeight-TextSize.height, TextSize.width, TextSize.height);

}

/* 进行绘制*/

/*

或者

- (void)drawAtPoint:(CGPoint)point withAttributes:(nullable NSDictionary<NSString *, id> *)attrs NS_AVAILABLE(10_0, 7_0);

*/

[self.text drawInRect:drawRect withAttributes:self.attrs];

}

@end

其使用方法:

#pragma mark -- 测试

-(void)testMakeUI{

UILabelView * firstLabel = [[UILabelView alloc]initWithFrame:CGRectMake(60, 64,200, 30)];

/*

为了能都让你们看到,文字的位置,所以这里设置为红色,你可以通过下面的方式来改变背景:

firstLabel.backgroundColor = [UIColor whiteColor];

*/

/* 注意:如果您没有设置绘制的内容,系统将会抛出一个异常提示:

2016-10-12 16:27:48.172 UILabel 的文字对齐Demo[2617:987072] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '绘制内容不能为空'

*/

firstLabel.text = @"成功QQ吧";

/*如果您不设置文字的显示形式,默认显示为中上部*/

firstLabel.styple = TextStyleCenter;

/* 要改变文字的颜色/字大小*/

NSDictionary * Dict = @{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:[UIFont systemFontOfSize:30]};

firstLabel.attrs =Dict;

[self.view addSubview:firstLabel];

}

第二种,有限制。要求是系统默认大小字体,矩形框和字体一样大。

方法是:

#pragma mark -- NSMutableAttributedString 的使用来改变字的位置

-(NSMutableAttributedString*)ChangeTextSeatObject:(UILabel*)objectLabel contentString:(NSString*)string top_Or_Bottom:(BOOL)top_Or_Bottom{

NSMutableAttributedString *  AttributedString = [[NSMutableAttributedString alloc]initWithString:string];

CGFloat TextHeightFloat = [string boundingRectWithSize:objectLabel.bounds.size options:NSStringDrawingUsesDeviceMetrics attributes:nil context:nil].size.height;

NSAssert(objectLabel!=nil, @"对象不能为空");

/* 参数的配置*/

float temp = 0.0;

if (top_Or_Bottom) {

temp = 1;

}else{

temp = -1;

}

[AttributedString  setAttributes:@{NSBaselineOffsetAttributeName:@(temp *(objectLabel.bounds.size.height - TextHeightFloat)*0.5)} range:NSMakeRange(0, string.length)];

return AttributedString;

}

UILable的字体位置设置(上、中、下等)相关推荐

  1. unity 发光字体_Unity3D 设置OnGUI中的字体样式、字体颜色、字体大小等

    1:字体样式 从系统盘,如C:\Windows\Fonts找一个.ttf格式的字体库,放到Assets目录下. 此时选中该字体库,在Inspector面板可以修改字体的大小,试了一下修改字体的颜色(F ...

  2. markdown字体大小设置_markdown中数学公式字体大小的方法

    题外话,这个用法咱不大推荐,相当于两个语言之间发生化学反应,剩下的全是玄学. 对于Typora用户提醒一下,这个写法可以正常渲染,但是如果在这之上尝试进行编辑则会乱套,不好用. 比如刚刚斩获的全新发现 ...

  3. android里面字体的设置,Android中字体设置Font

    1.一般使用默认字体,可以使用以下四种安卓系统提供的字体. android:typeface="normal" android:typeface="sans" ...

  4. ArchieOpenGL教程第11课:使用位图字体 在屏幕上显示字体

    第十一课A 源代码 图像字体A: 这一课我们将创建一些基于2D图像的字体,它们可以缩放,但不能旋转,并且总是面向前方,但作为基本的显示来说,我想已经够了. 使用位图字体比起使用图形字体(贴图)看起来不 ...

  5. 电脑 及 电脑应用软件背景色、字体的设置(护眼色)

    2019独角兽企业重金招聘Python工程师标准>>> 1.电脑背景色的设置: Window->Preferences->General->Editors-> ...

  6. android button 中文字位置设置,iOS开发-- 设置UIButton的文字显示位置、字体的大小、字体的颜色...

    btn.frame = CGRectMake(x, y, width, height); [btn setTitle: @"search" forState: UIControlS ...

  7. 在html中字怎么修改位置,css怎么设置字体位置?

    css怎么设置字体位置?下面本篇文章给大家介绍一下使用CSS设置字体位置的方法.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 方法1:使用text-align属性设置字体的位置 t ...

  8. Mac OS X上的不同字体位置及功能

    Mac OS X上的不同字体位置及功能 字体使用 字体文件夹位置 描述 "用户" ~/资源库/Fonts/ 每个用户可完全控制其个人文件夹中安装的字体.用户登录电脑后,这些字体即可 ...

  9. 设置Eclipse中的Courier New字体

    第一步就是如何设置字体: 1. 首先打开Eclipse,然后再打开菜单中的"Window",在下拉菜单项中选择最后一个Preferences: 2. 在弹出的窗体中,选择第一个Ge ...

最新文章

  1. sublime tex创建可服用的片段
  2. [PHP] 算法-数组重复数字统计的PHP实现
  3. appium desktop 1.7 byName不能用,重写
  4. 搭建一套ASP.NET Core+Nacos+Spring Cloud Gateway项目
  5. SignalR ——Android实践
  6. 进程的创建与可执行程序的加载
  7. leetcode162. 寻找峰值 变种二分见过吗
  8. Maven的核心笔记(3)常用命令、坐标和仓库
  9. LayaAir UI 组件 # Image 位图、Label 标签
  10. 阶段2 JavaWeb+黑马旅游网_15-Maven基础_第1节 基本概念_02maven依赖管理的概念
  11. 23种经典设计模式UML类图汇总
  12. matlab太阳影子定位,基于MATLAB软件的太阳影子定位
  13. 多空对比(DKDB)指标
  14. vmware虚拟机搭建网络拓扑教程
  15. php滚动公告栏,jQuery实现上下滚动公告栏详细代码
  16. html怎么设置seo,简单说明一下html相关的seo设置!
  17. 阿里云服务器ECS windows server已开放端口但连不上的问题
  18. Microsoft Edge浏览器不显示收藏夹栏 解决方法
  19. 产品设计公司该如何做好产品创新?
  20. 传Apple开发TD版iPhone,使用Marvell方案

热门文章

  1. [渝粤教育] 南京工业职业技术大学 传感与智能控制技术 参考 资料
  2. 孙鑫C++课程笔记2——创建类
  3. 美国技术人员常用招聘求职网站
  4. GIS篇—搭建本地地图服务器(geoserver )和 leaflet 绘制多边形,点击事件等
  5. Android Studio 制作商品销售记录APP
  6. 国内芯片厂家种类(更新中)
  7. 火车头如何把标题加html标签,火车头采集中内容页及标签Xpath可视化提取功能的使用...
  8. 什么是Profibus-DP转光纤_Profibs DP总线光端机
  9. OriginLab2015安装过程中注意点总结(二)
  10. 在凭证保存时 计算机程序能够对,湖北2015会计从业《会计电算化》全真预测卷三...