`
stephen830
  • 浏览: 2968144 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

iOS-在画布上写文字

 
阅读更多

iOS-在画布上写文字

 

有时候需要自定义一个UIView,然后在自定义的UIView上输出一段文字。如下面的例子和演示图。

需要注意的是以下几点:

1. ios7 采用方法 [_text drawInRect:rect withAttributes:attribute]; 而ios7之前是采用[_text drawInRect:self.bounds withFont:font]。

2.CGSize sizeText = [_text boundingRectWithSize:self.bounds.size
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:@{
                                                        NSFontAttributeName:font,//设置文字的字体
                                                        NSKernAttributeName:@10,//文字之间的字距
                                                        }
                                              context:nil].size 用来计算单行或多行文字的高度和宽度

 

 

ZJQView02.h

#import <UIKit/UIKit.h>

@interface ZJQView02 : UIView

@property (nonatomic,strong) NSString* text;

@end

 

ZJQView02.m

#import "ZJQView02.h"
#define IOS7 [[[UIDevice currentDevice]systemVersion] floatValue] >= 7.0 //判断SDK版本号是否是7.0或7。0以上

@implementation ZJQView02

-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {}
    return self;
}

-(void)drawRect:(CGRect)rect{
    //An opaque type that represents a Quartz 2D drawing environment.
    //一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你可以在上面任意绘画
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    /*写文字*/
    UIFont  *font = [UIFont boldSystemFontOfSize:12.0];//定义默认字体
    if (IOS7) {
        NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
        paragraphStyle.alignment=NSTextAlignmentCenter;//文字居中:发现只能水平居中,而无法垂直居中
        NSDictionary* attribute = @{
                                    NSForegroundColorAttributeName:[UIColor redColor],//设置文字颜色
                                    NSFontAttributeName:font,//设置文字的字体
                                    NSKernAttributeName:@10,//文字之间的字距
                                    NSParagraphStyleAttributeName:paragraphStyle,//设置文字的样式
                                    };
        
        //计算文字的宽度和高度:支持多行显示
        CGSize sizeText = [_text boundingRectWithSize:self.bounds.size
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:@{
                                                        NSFontAttributeName:font,//设置文字的字体
                                                        NSKernAttributeName:@10,//文字之间的字距
                                                        }
                                              context:nil].size;
        
        CGFloat width = self.bounds.size.width;
        CGFloat height = self.bounds.size.height;
        
        //为了能够垂直居中,需要计算显示起点坐标x,y
        CGRect rect = CGRectMake((width-sizeText.width)/2, (height-sizeText.height)/2, sizeText.width, sizeText.height);
        [_text drawInRect:rect withAttributes:attribute];
    }else{
        CGContextSetRGBFillColor (context,  1, 0, 0, 1.0);//设置填充颜色:红色
        [_text drawInRect:self.bounds withFont:font];
    }
}

@end

 

 

 

ViewController990.h

#import <UIKit/UIKit.h>
#import "ZJQView02.h"

@interface ViewController990 : UIViewController
{
@private
    
    ZJQView02* zjqView02;
}
@end

 

ViewController990.m

#import "ViewController990.h"
#import "MyLog.h"
#define IOS7 [[[UIDevice currentDevice]systemVersion] floatValue] >= 7.0 //判断SDK版本号是否是7.0或7。0以上


@interface ViewController990 ()

@end

@implementation ViewController990

- (void)viewDidLoad {
    [super viewDidLoad];
    [self doInit];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    zjqView02.center=CGPointMake(self.view.center.x, (self.view.bounds.size.height-zjqView02.frame.origin.y)/2);//移到屏幕中心点
    [MyLog logViews:self.view.window];
}

-(void) doInit {
    //IOS7版本特殊处理
    if (IOS7) {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    self.view.backgroundColor=[UIColor whiteColor];
    zjqView02 = [[ZJQView02 alloc]initWithFrame:CGRectMake(50, 50, 200, 60)];
    zjqView02.backgroundColor=[UIColor yellowColor];
    zjqView02.text=@"我是测试文字我是测试文字";
    
    [self.view addSubview:zjqView02];
}

@end

 

执行效果如下:



 

 项目工程请参考附近中的demo010.zip

 

 

 

 

 

 

 

 

 

 

  • 大小: 39.2 KB
分享到:
评论

相关推荐

    mindnode for mac

    - 直接在画布上创建和编辑新的节点 - 在大纲视图中的搜索和查看节点 - 自由地重新排列你的想法 - 精心设计的多点触控互动 - 全切,复制和粘贴的支持 - 支持iOS设备上VGA输出 - 全屏模式支持横向模式 - 自动扩展工作...

    react-native-sketch-canvas:一个通过在iOS和Android上触摸来进行绘图的React Native组件

    React本机素描画布 一个通过在iOS和Android上触摸来进行绘图的React Native... 可以在画布上绘制多个多行文字。 安装 从npm安装(仅支持RN&gt; = 0.40) npm install @terrylinla/react-native-sketch-canvas --save 链

    HTML5画布实现的超酷文字弹跳球效果

    今天我们分享一个来超酷弹跳球效果,这里我们使用纯HTML5的画布来实现动画及其图形。整个效果使用小球来组合生成字体,如果你的鼠标逼近这些小球,它们会四散...已覆盖Android、iOS、WP、Symbian、Kjava操作系统平台。

    ios和android设计尺寸规范1

    摘要:刚开始接触UI的时候,碰到的最多的就是尺寸问题,什么画布要建多大,文字该用多大才合适,我要做几套界面才可以?什么七七八八的也着实让人有些头疼。废话不多说,

    Canvas-Prompter:JavaScriptHTML5基于Canvas的提词器

    #Canvas Prompter Canvas Prompter旨在用作简单的提词器,它应可在Mac,PC和iOS / android上的浏览器中使用。 #demo在有一个简单的演示#controls默认控件很简单: 空间:停止/开始X:翻转X轴Y:翻转Y轴M:使“画布”...

    LinkedIdeas:一个macOSiOS应用程序,用于将想法视为概念的链接

    链接的想法 一个MacOS应用程序可以写下并连接想法,而iOS应用程序可以阅读文档。目的Linked Ideas是一个应用程序,可帮助您记录笔记并在画布上“表达您的想法”。 人们的思维过程可能非常混乱,我们之间有类似的...

    AXURE9最新版,小版本号3646,序列号可用(win版本)

    关闭该功能后,选中元件时键入文字,会在该元件上快速添加文本(恢复成RP8版本时那样)。但这种快捷输入有个BUG,输入中文时,第一个字符会变成字母。因为还没变成输入状态,中文输入法都没启动。所以。。你就别关闭...

    clipjs:在方向改变时,滚动你回到你在内容中的位置

    请不要使用这个。 我已经很久没有更新它了,我怀疑我是否会更新。 这是个好主意,但执行不力。... 我已经在运行 iOS 7 的 iPod touch 上对其进行了测试。 缩小和连接的总文件大小小于 0.5KB,因此您绝对可以内联

    博客:技术资料日常积累(欢迎投稿)

    提高文字录入效率 最端的纯粹的分屏软件,办公必备窗口管理工具! 功能一应俱全,简单好用的线上PDF工具 将你的本地服务暴露到公网。 全功能照片图像编辑器使用画布。这真的很简单,而且它带有很好的过滤器 H5...

Global site tag (gtag.js) - Google Analytics