麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > iOS > 正文

iOS開發實戰之Label全方位對齊的輕松實現

2019-10-21 18:39:32
字體:
來源:轉載
供稿:網友

前言

本文主要給大家介紹了關于iOS Label全方位對齊的實現方法,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧

ARUILabelTextAlign

1. 實現 UILabel文本在 左(上 中 下)、中(上 中 下)、右(上 中 下) 9個方位顯示;

2. 提供富文本底部不對齊的解決方案;

iOS開發,Label,全方位對齊

演示

核心代碼:

ARAlignLabel.h

#import <UIKit/UIKit.h>@class ARMaker;typedef NS_ENUM(NSUInteger, textAlignType){ textAlignType_top = 10, // 頂部對齊 textAlignType_left,  // 左邊對齊 textAlignType_bottom,  // 底部對齊 textAlignType_right,  // 右邊對齊 textAlignType_center  // 水平/垂直對齊(默認中心對齊)};@interface ARAlignLabel : UILabel/** * 根據對齊方式進行文本對齊 * * @param alignType 對齊block */- (void)textAlign:(void(^)(ARMaker *make))alignType;@end//工具類@interface ARMaker : NSObject/* 存放對齊樣式 */@property(nonatomic, strong) NSMutableArray *typeArray;/** * 添加對齊樣式 */- (ARMaker *(^)(textAlignType type))addAlignType;@end

ARAlignLabel.m

#import "ARAlignLabel.h"@interface ARAlignLabel ()/* 對齊方式 */@property(nonatomic, strong) NSArray *typeArray;//上@property(nonatomic, assign) BOOL hasTop;//左@property(nonatomic, assign) BOOL hasLeft;//下@property(nonatomic, assign) BOOL hasBottom;//右@property(nonatomic, assign) BOOL hasRight;@end@implementation ARAlignLabel- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines { CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines]; if (self.typeArray){  for (int i=0; i<self.typeArray.count; i++) {   textAlignType type = [self.typeArray[i] integerValue];   switch (type) {    case textAlignType_top: //頂部對齊     self.hasTop = YES;     textRect.origin.y = bounds.origin.y;     break;    case textAlignType_left: //左部對齊     self.hasLeft = YES;     textRect.origin.x = bounds.origin.x;     break;    case textAlignType_bottom: //底部對齊     self.hasBottom = YES;     textRect.origin.y = bounds.size.height - textRect.size.height;     break;    case textAlignType_right: //右部對齊     self.hasRight = YES;     textRect.origin.x = bounds.size.width - textRect.size.width;     break;    case textAlignType_center:     if (self.hasTop) { //上中      textRect.origin.x = (bounds.size.width - textRect.size.width)*0.5;     }     else if (self.hasLeft) { //左中      textRect.origin.y = (bounds.size.height - textRect.size.height)*0.5;     }     else if (self.hasBottom) { //下中      textRect.origin.x = (bounds.size.width - textRect.size.width)*0.5;     }     else if (self.hasRight) { //右中      textRect.origin.y = (bounds.size.height - textRect.size.height)*0.5;     }     else{ //上下左右居中      textRect.origin.x = (bounds.size.width - textRect.size.width)*0.5;      textRect.origin.y = (bounds.size.height - textRect.size.height)*0.5;     }     break;    default:     break;   }  } } return textRect;}- (void)drawTextInRect:(CGRect)requestedRect { CGRect actualRect = requestedRect; if (self.typeArray) {  actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines]; } [super drawTextInRect:actualRect];}- (void)textAlign:(void(^)(ARMaker *make))alignType { ARMaker *make = [[ARMaker alloc]init]; alignType(make); self.typeArray = make.typeArray;}@end//工具類@implementation ARMaker- (instancetype)init { self = [super init]; if (self) {  self.typeArray = [NSMutableArray array]; } return self;}- (ARMaker *(^)(enum textAlignType type))addAlignType { __weak typeof (self) weakSelf = self; return ^(enum textAlignType type) {  [weakSelf.typeArray addObject:@(type)];  return weakSelf; };}@end

工具使用 - 九個方位對齊

- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor];  if (_index == 9) {  //富文本底部對齊  [self attributedTextAgainOfBottom]; }else {  ARAlignLabel *label = [[ARAlignLabel alloc] initWithFrame:CGRectMake(kScreenWidth/2.0 - 150, 300, 300, 80)];  label.backgroundColor = [UIColor orangeColor];  label.textColor = [UIColor blackColor];  label.font = [UIFont systemFontOfSize:18];  label.text = @"愛學習,愛編程,愛咖啡可樂";  label.numberOfLines = 1;  [self.view addSubview:label];    switch (_index) {   case 0:    [label textAlign:^(ARMaker *make) {     make.addAlignType(textAlignType_left).addAlignType(textAlignType_top);    }];    break;   case 1:    [label textAlign:^(ARMaker *make) {     make.addAlignType(textAlignType_left).addAlignType(textAlignType_center);    }];    break;   case 2:    [label textAlign:^(ARMaker *make) {     make.addAlignType(textAlignType_left).addAlignType(textAlignType_bottom);    }];    break;   case 3:    [label textAlign:^(ARMaker *make) {     make.addAlignType(textAlignType_center).addAlignType(textAlignType_top);    }];    break;   case 4:    [label textAlign:^(ARMaker *make) {     make.addAlignType(textAlignType_center);    }];    break;   case 5:    [label textAlign:^(ARMaker *make) {     make.addAlignType(textAlignType_center).addAlignType(textAlignType_bottom);    }];    break;   case 6:    [label textAlign:^(ARMaker *make) {     make.addAlignType(textAlignType_right).addAlignType(textAlignType_top);    }];    break;   case 7:    [label textAlign:^(ARMaker *make) {     make.addAlignType(textAlignType_right).addAlignType(textAlignType_center);    }];    break;   case 8:    [label textAlign:^(ARMaker *make) {     make.addAlignType(textAlignType_right).addAlignType(textAlignType_bottom);    }];    break;   default:    break;  } }}

富文本底部對齊

//富文本底部對齊- (void)attributedTextAgainOfBottom {  CGFloat space = 10.0;  ARAlignLabel *leftLB = [[ARAlignLabel alloc] initWithFrame:CGRectMake(20, 200, kScreenWidth/2.0 - 20 - space/2.0, 80)]; leftLB.backgroundColor = [UIColor lightGrayColor]; leftLB.textColor = [UIColor blackColor]; leftLB.numberOfLines = 1; [self.view addSubview:leftLB]; //右下 [leftLB textAlign:^(ARMaker *make) {  make.addAlignType(textAlignType_center); }];  NSMutableAttributedString *attributedArr = [[NSMutableAttributedString alloc] initWithString:@"單價 $123"]; [attributedArr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:40], NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(0, 1)]; [attributedArr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:25], NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(1, 1)]; [attributedArr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20], NSForegroundColorAttributeName:[UIColor blueColor]} range:NSMakeRange(3, 1)]; [attributedArr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:35], NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(4, attributedArr.length - 4)];  leftLB.attributedText = attributedArr;   //對齊之后 ARAlignLabel *rightLB = [[ARAlignLabel alloc] initWithFrame:CGRectMake(kScreenWidth/2.0 + space/2.0, 200, leftLB.frame.size.width, 80)]; rightLB.backgroundColor = [UIColor lightGrayColor]; rightLB.textColor = [UIColor blackColor]; rightLB.numberOfLines = 1; [self.view addSubview:rightLB]; //左下 [rightLB textAlign:^(ARMaker *make) {  make.addAlignType(textAlignType_center); }];  //設置部分文字的偏移量 (0是讓文字保持原來的位置, 負值是讓文字下移,正值是讓文字上移) [attributedArr addAttribute:NSBaselineOffsetAttributeName value:@(1) range:NSMakeRange(0, 1)]; [attributedArr addAttribute:NSBaselineOffsetAttributeName value:@(0) range:NSMakeRange(1, 1)]; [attributedArr addAttribute:NSBaselineOffsetAttributeName value:@(-2) range:NSMakeRange(3, 1)]; [attributedArr addAttribute:NSBaselineOffsetAttributeName value:@(-3) range:NSMakeRange(4, attributedArr.length - 4)];  rightLB.attributedText = attributedArr; }

富文本底部對齊 - 使用場景:

iOS開發,Label,全方位對齊

Github:https://github.com/ArchLL/ARUILabelTextAlign

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 91成人一区二区三区 | 欧美日韩视频网站 | 日本欧美一区二区 | 精品国产一区二区三区久久久蜜月 | 毛片免费观看视频 | 一级α片免费看刺激高潮视频 | 国产又粗又爽又深的免费视频 | 国产乱xxxx| 国产精品毛片va一区二区三区 | 在线日韩av电影 | 在线影院av | 色偷偷一区 | 国产精品av久久久久久久久久 | 精品国产一区二区久久 | 国产三级精品最新在线 | xxx日本视频| 美女视频黄a视频免费全过程 | 一区二区久久电影 | 在线观看国产一区二区 | 亚洲一区二区中文字幕在线观看 | 国产分类视频 | 成人一级黄色大片 | 黄色特级一级片 | hd性videos意大利复古 | 久久精品中文字幕一区 | 免费一级电影 | 日韩视频区| 视频在线91| 在线亚洲播放 | 日韩视频在线不卡 | 成人精品久久久 | 色婷婷久久久亚洲一区二区三区 | 2021狠狠操| 黄色片免费在线 | 久草在线高清 | 日本教室三级在线看 | 毛片视频免费观看 | 露脸各种姿势啪啪的清纯美女 | 一区二区三视频 | 国产精品美女久久久免费 | 日韩电影一区二区 |