美文网首页
iOS:设置圆角和阴影、富文本显示所有的子串

iOS:设置圆角和阴影、富文本显示所有的子串

作者: 春暖花已开 | 来源:发表于2019-04-01 13:28 被阅读0次

我们都知道,常规方法不能同时对一个控件设置圆角和阴影,提供一个解决方法:在外面包裹一个 view

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [super viewDidLoad];
    
    UIView *shadowView = [[UIView alloc]initWithFrame:CGRectMake(60, 60, 100, 100)];
    shadowView.layer.cornerRadius = 50;
    shadowView.layer.shadowOffset = CGSizeMake(2, 5);
    shadowView.layer.shadowOpacity = 1;
    shadowView.layer.shadowColor = [UIColor blueColor].CGColor;
    [self.view addSubview:shadowView];
    
    UIButton *cornerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cornerBtn.frame = CGRectMake(0, 0, 100, 100);
    cornerBtn.backgroundColor = [UIColor redColor];
    [cornerBtn setTitle:@"圆角+阴影" forState:UIControlStateNormal];
    cornerBtn.layer.cornerRadius = 50;
    //    messageBtn.layer.masksToBounds = YES;
    [shadowView addSubview:cornerBtn];
}
图片.png

参考:
iOS-Core-Animation-Advanced-Techniques


富文本显示所有子串
#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UILabel *showLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.showLabel];
    
    self.showLabel.attributedText = [self highlightText:@"123" fromSrcText:@"432123678912367"];
}

/**
 富文本显示
 @param highlightText 要高亮的字符串
 @param srcText 要操作的字符串
 */
- (NSMutableAttributedString *)highlightText:(NSString *)highlightText fromSrcText:(NSString *)srcText {
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:srcText];
    
    NSMutableArray *rangeArray = [self getRangesFromOriginStr:srcText subStr:highlightText];
    for (NSInteger i = 0; i < rangeArray.count; i++) {
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:[rangeArray[i] rangeValue]];
    }
    
    return attrStr;
}

- (NSMutableArray *)getRangesFromOriginStr:(NSString *)orignStr subStr:(NSString *)subStr {
    
    int location = 0;
    NSMutableArray *rangeArray = [NSMutableArray array];
    NSRange range = [orignStr rangeOfString:subStr];
    if (range.location == NSNotFound){
        return rangeArray;
    }
    
    BOOL isFirst = YES;
    while (range.location != NSNotFound) {
        if (location == 0 && isFirst) {
            isFirst = NO;
            location += range.location;
        } else {
            location += range.location + subStr.length;
        }
        //记录位置
        NSValue *value = [NSValue valueWithRange:NSMakeRange(location, subStr.length)];
        [rangeArray addObject:value];
        orignStr = [orignStr substringFromIndex:range.location + range.length];
        range = [orignStr rangeOfString:subStr];
    }
    return rangeArray;
}

- (UILabel *)showLabel {
    if (!_showLabel) {
        _showLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 350, 30)];
    }
    return _showLabel;
}

@end
目标字符串在串中
目标字符串在开头

相关文章

网友评论

      本文标题:iOS:设置圆角和阴影、富文本显示所有的子串

      本文链接:https://www.haomeiwen.com/subject/yeqvbqtx.html