iOS 设置一行颜色不同的NSString 、剪切图片、设置TableView的自定义header

iOS开发技巧集锦
本文介绍了iOS开发中几种实用的技术实现方法,包括如何使用NSAttributedString高亮显示特定文本、按指定规格裁剪图片以及如何在UITableView的头部添加自定义UIView。

需求:将字符串:请阅读下面相关内容,进行下一步请点击:同意  ,中"同意"二字设置为红色

    NSMutableAttributedString *str=[[NSMutableAttributedString alloc]initWithString:@"请阅读下面相关内容,进行下一步请点击:同意"];
//NSRange:要改变的位置
    NSRange redRange=NSMakeRange([[str string]rangeOfString:@"同意"].location, [[str string]rangeOfString:@"同意"].length);
//value  :要改变的颜色
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
    
    [self.label setAttributedText:str];
    

需求:图片按规格剪切


     UIImage *image1=[UIImage imageNamed:@"icon"];
    
     CGRect frame = self.image1.bounds;
//注意:这里是按照像素剪切,所以要在width 、height后面乘2
    CGImageRef finalImgRef = CGImageCreateWithImageInRect(image1.CGImage, CGRectMake(0, 0, frame.size.width*2, frame.size.height*2));
    
    
    
    

需求:在每组TV的header位置添加自定义的UIView

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==0) {
        UIView *header =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
        header.backgroundColor=[UIColor redColor];
        return header;
    }else if(section==1){
   

    Header *hview=[[[NSBundle mainBundle]loadNibNamed:@"header" owner:self options:nil]lastObject];
    return hview;
  }else{
    return [UIView alloc];
}


}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 100;
}
//二者缺一不可

 

 

 

转载于:https://my.oschina.net/sgcllr/blog/793346

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值