如何在iPhone上创建圆角UILabel? [英] How do I create a round cornered UILabel on the iPhone?

查看:115
本文介绍了如何在iPhone上创建圆角UILabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有内置的方式来创建圆角UILabels?如果答案是否定的,那么如何创建这样的对象呢?

Is there a built in way to create round-cornered UILabels? If the answer is no, how would one go about creating such an object?

推荐答案

3.0之前



一种方法,最近使用的是创建一个UIView子类,它简单地绘制一个圆角矩形,然后使UILabel或在我的情况下,UITextView,它的一个子视图。具体来说:

Before 3.0

One way to do it, which I used recently, is to create a UIView subclass which simply draws a rounded rectangle, and then make the UILabel or, in my case, UITextView, a subview inside of it. Specifically:


  1. 创建 UIView 子类,并命名为 RoundRectView 。 $ c $ c>方法,使用Core Graphics调用(如CGContextAddLineToPoint()用于边缘和CGContextAddArcToPoint()用于圆角)在视图的边界周围绘制路径。

  2. 创建 UILabel 实例,并将其作为RoundRectView的子视图。

  3. 将标签的框架设置为RoundRectView边界。 (例如, label.frame = CGRectInset(roundRectView.bounds,8,8);

  1. Create a UIView subclass and name it something like RoundRectView.
  2. In RoundRectView's drawRect: method, draw a path around the bounds of the view using Core Graphics calls like CGContextAddLineToPoint() for the edges and and CGContextAddArcToPoint() for the rounded corners.
  3. Create a UILabel instance and make it a subview of the RoundRectView.
  4. Set the frame of the label to be a few pixels inset of the RoundRectView's bounds. (For example, label.frame = CGRectInset(roundRectView.bounds, 8, 8);)


b $ b

如果创建一个通用的UIView,然后使用检查器更改其类,您可以使用Interface Builder将RoundRectView放置在视图上。在编译和运行应用程序之前,您将看不到矩形,但至少您可以放置​​子视图,并将其连接到插座或操作(如果需要)。

You can place the RoundRectView on a view using Interface Builder if you create a generic UIView and then change its class using the inspector. You won't see the rectangle until you compile and run your app, but at least you'll be able to place the subview and connect it to outlets or actions if needed.

iPhone OS 3.0及更高版本支持上的 cornerRadius CALayer 类。每个视图都有一个 CALayer 实例,您可以操作。这意味着您现在可以在一行中获得圆角:

iPhone OS 3.0 and later supports the cornerRadius property on the CALayer class. Every view has a CALayer instance that you can manipulate. This means you can get rounded corners in one line now:

view.layer.cornerRadius = 8;

您需要 #import< QuartzCore / QuartzCore.h& / code>并链接到QuartzCore框架以访问CALayer的头和属性。

You will need to #import <QuartzCore/QuartzCore.h> and link to the QuartzCore framework to get access to CALayer's headers and properties.

这篇关于如何在iPhone上创建圆角UILabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆