最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS開發(fā)實(shí)戰(zhàn)之Label全方位對(duì)齊的輕松實(shí)現(xiàn)

 更新時(shí)間:2018年10月12日 09:37:57   作者:Metro追光者  
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)實(shí)戰(zhàn)之輕松實(shí)現(xiàn)Label全方位對(duì)齊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

本文主要給大家介紹了關(guān)于iOS Label全方位對(duì)齊的實(shí)現(xiàn)方法,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧

ARUILabelTextAlign

1. 實(shí)現(xiàn) UILabel文本在 左(上 中 下)、中(上 中 下)、右(上 中 下) 9個(gè)方位顯示;

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

演示

核心代碼:

ARAlignLabel.h

#import <UIKit/UIKit.h>
@class ARMaker;

typedef NS_ENUM(NSUInteger, textAlignType)
{
 textAlignType_top = 10, // 頂部對(duì)齊
 textAlignType_left,  // 左邊對(duì)齊
 textAlignType_bottom,  // 底部對(duì)齊
 textAlignType_right,  // 右邊對(duì)齊
 textAlignType_center  // 水平/垂直對(duì)齊(默認(rèn)中心對(duì)齊)
};

@interface ARAlignLabel : UILabel

/**
 * 根據(jù)對(duì)齊方式進(jìn)行文本對(duì)齊
 *
 * @param alignType 對(duì)齊block
 */
- (void)textAlign:(void(^)(ARMaker *make))alignType;

@end


//工具類
@interface ARMaker : NSObject

/* 存放對(duì)齊樣式 */
@property(nonatomic, strong) NSMutableArray *typeArray;

/**
 * 添加對(duì)齊樣式
 */
- (ARMaker *(^)(textAlignType type))addAlignType;

@end

ARAlignLabel.m

#import "ARAlignLabel.h"

@interface ARAlignLabel ()

/* 對(duì)齊方式 */
@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: //頂部對(duì)齊
     self.hasTop = YES;
     textRect.origin.y = bounds.origin.y;
     break;
    case textAlignType_left: //左部對(duì)齊
     self.hasLeft = YES;
     textRect.origin.x = bounds.origin.x;
     break;
    case textAlignType_bottom: //底部對(duì)齊
     self.hasBottom = YES;
     textRect.origin.y = bounds.size.height - textRect.size.height;
     break;
    case textAlignType_right: //右部對(duì)齊
     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

工具使用 - 九個(gè)方位對(duì)齊

- (void)viewDidLoad {
 [super viewDidLoad];
 self.view.backgroundColor = [UIColor whiteColor];
 
 if (_index == 9) {
  //富文本底部對(duì)齊
  [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 = @"愛學(xué)習(xí),愛編程,愛咖啡可樂";
  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;
  }
 }
}

富文本底部對(duì)齊

//富文本底部對(duì)齊
- (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:@"單價(jià) $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;
 
 
 //對(duì)齊之后
 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);
 }];
 
 //設(shè)置部分文字的偏移量 (0是讓文字保持原來的位置, 負(fù)值是讓文字下移,正值是讓文字上移)
 [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;
 
}

富文本底部對(duì)齊 - 使用場(chǎng)景:


Github:https://github.com/ArchLL/ARUILabelTextAlign (本地下載

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • iOS編程學(xué)習(xí)中關(guān)于throttle的那些事

    iOS編程學(xué)習(xí)中關(guān)于throttle的那些事

    這篇文章主要給大家介紹了關(guān)于iOS編程學(xué)習(xí)中throttle的那些事,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • iOS關(guān)鍵字static extern const使用示例詳解

    iOS關(guān)鍵字static extern const使用示例詳解

    這篇文章主要為大家介紹了iOS關(guān)鍵字static extern const使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • IOS開發(fā)中鍵盤輸入屏幕上移的解決方法

    IOS開發(fā)中鍵盤輸入屏幕上移的解決方法

    在IOS開法中經(jīng)常會(huì)遇到鍵盤遮擋屏幕的事情,經(jīng)常檔住下面的按鈕,下面小編給大家分享IOS開發(fā)中鍵盤輸入屏幕上移的解決方法,感興趣的朋友一起看看吧
    2016-10-10
  • iOS中獲取系統(tǒng)相冊(cè)中的圖片實(shí)例

    iOS中獲取系統(tǒng)相冊(cè)中的圖片實(shí)例

    這篇文章主要介紹了iOS中獲取系統(tǒng)相冊(cè)中的圖片實(shí)例,具有一定的參考價(jià)值沒有需要的朋友可以了解一下。
    2016-11-11
  • iOS開發(fā)之如何給View添加指定位置的邊框線詳解

    iOS開發(fā)之如何給View添加指定位置的邊框線詳解

    這篇文章主要給大家介紹了iOS開發(fā)之如何給View添加指定位置的邊框線的相關(guān)資料,給view加邊框很容易,重點(diǎn)是如何給指定邊框加邊框,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-10-10
  • IOS 長(zhǎng)鏈接與短鏈接之間的轉(zhuǎn)換

    IOS 長(zhǎng)鏈接與短鏈接之間的轉(zhuǎn)換

    這篇文章主要介紹了IOS 長(zhǎng)鏈接與短鏈接之間的轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • iOS動(dòng)畫教你編寫Slack的Loading動(dòng)畫進(jìn)階篇

    iOS動(dòng)畫教你編寫Slack的Loading動(dòng)畫進(jìn)階篇

    這篇文章主要為大家進(jìn)一步詳細(xì)介紹了iOS動(dòng)畫教你編寫Slack的Loading動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • iOS獲取網(wǎng)絡(luò)類型的方法匯總

    iOS獲取網(wǎng)絡(luò)類型的方法匯總

    本篇文章主要給大家匯總介紹了iOS獲取網(wǎng)絡(luò)類型的方法,主要涉及到方面的內(nèi)容,對(duì)于IOS開發(fā)感興趣的同學(xué)可以參考一下
    2015-06-06
  • iOS中textField限制字符串長(zhǎng)度、字符數(shù)的方法

    iOS中textField限制字符串長(zhǎng)度、字符數(shù)的方法

    這篇文章主要給大家介紹了關(guān)于iOS中textField限制字符串長(zhǎng)度、字符數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • MacOS系統(tǒng)下Unity啟動(dòng)黑屏的解決方法

    MacOS系統(tǒng)下Unity啟動(dòng)黑屏的解決方法

    最近發(fā)現(xiàn)了一個(gè)問題,unity一打開就黑屏,通過查找相關(guān)的資料終于解決了,所以下面這篇文章主要給大家介紹了關(guān)于在MacOS系統(tǒng)下Unity啟動(dòng)黑屏的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2018-01-01

最新評(píng)論

平南县| 子长县| 博罗县| 高雄市| 阿鲁科尔沁旗| 定边县| 长治县| 本溪| 竹北市| 定边县| 贵阳市| 南召县| 顺平县| 东方市| 札达县| 鄱阳县| 久治县| 东明县| 宜良县| 环江| 郓城县| 托里县| 周至县| 德清县| 福建省| 乐山市| 报价| 兰坪| 塔河县| 鸡泽县| 米林县| 温宿县| 钦州市| 海伦市| 陇南市| 大渡口区| 和田市| 平果县| 盘锦市| 江源县| 榕江县|