iOS UILabel控件默认文字位置是居中的,如图所示:

但是我们经常碰到这样的需求,希望文字向上置顶,或者向下置底,但是很遗憾,iOS API中并没有提供相应的属性和方法,需要我们手动设置。

利用 分类(category)为UILabel添加属性 isTop 和 isBottom来控制文字是否置顶和置底。

实现:利用往文字后面活前面下面添加”\n”来实现文字填充满整个UILable控件实现置顶/置顶效果

.h文件

#import <UIKit/UIKit.h>

@interface UILabel (TextAlign)

@property (nonatomic, assign) BOOL isTop;
@property (nonatomic, assign) BOOL isBottom; @end

.m文件

#import "UILabel+TextAlign.h"

@implementation UILabel (TextAlign)

-(void)setIsTop:(BOOL)isTop {

    if (isTop) {

        CGSize fontSize = [self.text sizeWithFont:self.font];
//控件的高度除以一行文字的高度
int num = self.frame.size.height/fontSize.height;
//计算需要添加换行符个数
int newLinesToPad = num - self.numberOfLines;
self.numberOfLines = 0;
for(int i=0; i<newLinesToPad; i++)
//在文字后面添加换行符"/n"
self.text = [self.text stringByAppendingString:@"\n"];
}
} -(void)setIsBottom:(BOOL)isBottom { if (isBottom) {
CGSize fontSize = [self.text sizeWithFont:self.font];
//控件的高度除以一行文字的高度
int num = self.frame.size.height/fontSize.height;
//计算需要添加换行符个数
int newLinesToPad = num - self.numberOfLines;
self.numberOfLines = 0;
for(int i=0; i<newLinesToPad; i++)
//在文字前面添加换行符"/n"
self.text = [NSString stringWithFormat:@" \n%@",self.text];
}
} @end

使用方法: 
导入头文件

#import "UILabel+TextAlign.h"

然后设置属性


//置顶
self.lb.isTop = YES; //置底
self.lb.isBottom = YES;
源码免费下载地址:http://www.jinhusns.com/Products/Download/

iOS UILabel 文字 置顶/置底 实现的更多相关文章

  1. iOS UILabel文字自适应高度自适应

    第一步:创建UILabel对象,并设置一些基本设置 UILabel *label = [[UILabel alloc] init]; label.text = @"8月29日,在雅加达亚运会 ...

  2. iOS UILabel文字缩进

    使用NSMutableParagraphStyle实现label文字首尾的缩进 NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphSty ...

  3. jQuery 元素的选中, 置顶、上移、下移、置底、删除

    如截图: <ul> <li class="li01" onclick="C_columnSetTop(this)"><i>& ...

  4. js上移、下移、置顶、置底功能实现

    实现页面上列表内容上移.下移.置顶.置底 功能,主要实现思路是节点操作,比如说:上移,直接把点击项移动到前一个节点,以此类推,当然实际代码实现还要加些判断,如当前点击操作项是否已经是置底或置底了,如果 ...

  5. js滚动显示: 滚动条置顶/底

    <script> //当聊天室的内容超出页面范围时, 如何让页面刷新后 显示最下面的内容 document.getElementByIdx ( 'chatboard').scrollTop ...

  6. jquery中的置顶,置底,向上,向下的排序功能

    css .selectedLi{background: #f0ad4e;color:#fff;} html部分 <ul class="seetSelect2" id='sys ...

  7. FMX 窗口置顶 或置底 xe10

    FMX 窗口置顶  或置底 xe10 Popup 置底 StayOnTop 置顶

  8. IOS 作业项目 TableView两个section中cell置顶功能实现

    点击cell会置顶,其他的下移

  9. 【iOS开发系列】cell切割线置顶

    废话不多说,直接上代码: /** * tableViewCell切割线左側置顶 < 1 > */ -(void)viewDidLayoutSubviews { if ([_tableVie ...

随机推荐

  1. return *this和return this有什么区别?

    return *this返回的是当前对象的克隆或者本身(若返回类型为A, 则是克隆, 若返回类型为A&, 则是本身 ). return this返回当前对象的地址(指向当前对象的指针). 转: ...

  2. [转] GloVe公式推导

    from: https://pengfoo.com/post/machine-learning/2017-04-11 GloVe(Global Vectors for Word Representat ...

  3. Mac环境下Scrapy的安装

    直接命令安装: $ easy_install scrapy 从 GitHub 安装: $ git clonehttps://github.com/scrapy/scrapy.git $ cd scra ...

  4. 删除Win10资源管理器中的3D对象/音乐/视频文件夹

    Win10如何删除资源管理器中的3D对象/音乐/视频等文件夹?使用Win10系统的用户都知道,打开此电脑之后,资源管理上面会显示文档/音乐/视频等7个文件夹,一些用户认为很少使用到它们,想要除之而后快 ...

  5. patch 28729262

    打补丁最后出个error OPatch found the word "error" in the stderr of the make command.Please look a ...

  6. 收藏vue技术内幕

    http://hcysun.me/vue-design/art/7vue-reactive.html#observer-%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0

  7. python正则表达式--split、sub、escape方法

    1.re.split 语法: re.split(pattern, string[, maxsplit=0, flags=0]) 参数: pattern    匹配的正则表达式 string      ...

  8. js圆形头像实现

    定义CSS <style> .to{width:100px;height:100px;border-radius:100px} </style> 这样就实现了 主要是borde ...

  9. uni-app 在input获取焦点(弹出软键盘后收起软键盘),页面不下滑,留下下方空白

    加入收起软键盘时让页面回正 uni.pageScrollTo({ scrollTop: 0, duration: 0 });

  10. 无法获得VMCI驱动程序的版本:句柄无效

    解决方法: 查找到 vmci0.present="TRUE" 代码,将TRUE更改为FALSE保存即可