UITextKit框架的学习(一)

本文介绍了如何利用UITextKit框架中的NSAttributedString属性实现图文混排,包括字体、颜色、背景、连体字符等属性的设置,并提到了支持富文本编辑的控件如UITextView。
摘要由CSDN通过智能技术生成

利用NSTextAttachment实现简单的图文混排

    //添加UItextView,NSTextView是支持富文本编辑器的控件
    UITextView *label=[[UITextView alloc]initWithFrame:CGRectMake(20, 50, self.view.frame.size.width-40, 300)];
    [self.view addSubview:label];

    //设置内容
    NSMutableAttributedString *string =[[NSMutableAttributedString alloc]initWithString:@"123456789101112" attributes:nil];

    NSTextAttachment *textAttachment = [[ NSTextAttachment alloc ] initWithData:nil ofType:nil ] ;
    UIImage * smileImage = [UIImage imageNamed:@"test.jpg"];

//    方法1
      textAttachment.image = [self image:smileImage byScalingToSize:CGSizeMake(20,20)];

//    方法2
//    textAttachment.image =smileImage;
//    textAttachment.bounds=CGRectMake(0, 0, 20, 20);

    NSAttributedString * textAttachmentString = [ NSAttributedString attributedStringWithAttachment:textAttachment ] ;

    [ string insertAttributedString:textAttachmentString atIndex:6 ] ;

    label.attributedText = string ;
#pragma makr-返回修改大小后的图片
- (UIImage *)image:(UIImage*)image byScalingToSize:(CGSize)targetSize {
    UIImage *sourceImage = image;
    UIImage *newImage = nil;

    UIGraphicsBeginImageContext(targetSize);

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = CGPointZero;
    thumbnailRect.size.width  = targetSize.width;
    thumbnailRect.size.height = targetSize.height;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage ;
}

这里写图片描述


AttributedString属性参照
// NSFontAttributeName 设置字体属性,默认值:字体

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值