图片拉伸技巧

来源:互联网 发布:淘宝可以卖教程吗 编辑:程序博客网 时间:2024/05/29 09:18

图片拉伸技巧

         *设置图标范围内的拉伸,保证自己想要的部分不变,有三种方法.

         *方法一:

          right = width - leftCapWidth - 1;bottom =height - topCapWidth - 1

          [textBGstretchableImageWithLeftCapWidth:(NSInteger)           

                                   topCapHeight:(NSInteger)]

         *方法二:

         设置UIEdgeInsets属性.取要拉伸的部分.

         [textBGresizableImageWithCapInsets:(UIEdgeInsets)]

         *方法三:

         设置UIEdgeInsets属性,和填充的样式UIImageResizingMode

         [textBGresizableImageWithCapInsets:(UIEdgeInsets)

resizingMode:(UIImageResizingMode)];

 

举例子:

 

        //自己发的是蓝色

        UIImage *textBG = [UIImageimageNamed:@"chat_send_nor"];

       

        CGFloat selectW =textBG.size.width*0.5;//取图横向中心位置

        CGFloat selectH =textBG.size.height*0.5;//取图高中心位置

       

        //取原图中,中心位置,来将扩大的部分填充.

        UIImage *realTextBG = [textBGresizableImageWithCapInsets:UIEdgeInsetsMake(selectH,selectW,selectH,selectW)];

       

        //将图添加上

        [self.textViewsetBackgroundImage:realTextBG

forState:UIControlStateNormal];

 

文本计算尺寸

/**

 *计算文字尺寸

 * text     需要计算尺寸的文字

 * font     文字的字体

 * maxSize  文字的最大尺寸

 */

-(CGSize)sizeWithText:(NSString*)text font:(UIFont *)font maxSize:(CGSize)maxSize{  

    NSDictionary *attr =@{NSFontAttributeName:font};    //用字典接受

return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;

//计算出文本的大小}

0 0
原创粉丝点击