5
问题
我试图在Swift操场上制作一个自定义的UIView组件。我已经能够弄清楚除了正确呈现文本之外,如何做所有事情。我不确定我是否应该通过Core Graphics或Core Text来做到这一点,或者确切地说如何让它正常工作。Swift - Playground - 核心图形/核心文本/自定义视图
正如你可以在图片中看到下面我要呈现的文本都倒过来,正面朝上放在我的自定义视图部件的两侧
我已经试过各种方法来获得文字工作,但我一直搁浅。如果有人能够告诉我如何修改我的操场以添加一些很棒的文字渲染。
游乐场代码
// Playground - noun: a place where people can play
import Foundation
import UIKit
class RunwayView : UIView {
var northText : String?
var southText : String?
///Draws the "runway"
override func drawRect(rect: CGRect) {
// Setup graphics context
var ctx = UIGraphicsGetCurrentContext()
// clear context
CGContextClearRect(ctx, rect)
let parentVieBounds = self.bounds
let width = CGRectGetWidth(parentVieBounds)
let height = CGRectGetHeight(parentVieBounds)
// Setup the heights
let endZoneHeight : CGFloat = 40
/* Remember y is negative and 0,0 is upper left*/
//Create EndZones
CGContextSetRGBFillColor(ctx, 0.8, 0.8, 0.8, 1)
CGContextFillRect(ctx, CGRectMake(0, 0, width, endZoneHeight))
CGContextFillRect(ctx, CGRectMake(0, height-endZoneHeight, width, endZoneHeight))
var northString = NSMutableAttributedString(string: "36")
var attrs = [NSFontAttributeName : UIFont.systemFontOfSize(16.0)]
var gString = NSMutableAttributedString(string:"g", attributes:attrs);
var line = CTLineCreateWithAttributedString(gString)
CGContextSetTextPosition(ctx, 10, 50);
CTLineDraw(line, ctx);
// Clean up
}
}
var outerFrame : UIView = UIView(frame: CGRectMake(20,20,400,400))
var runway1 : RunwayView = RunwayView(frame: CGRectMake(0,0,30,260))
var runway2 : RunwayView = RunwayView(frame: CGRectMake(80,0,30,340))
runway1.transform = CGAffineTransformConcat(CGAffineTransformMakeTranslation(20, 200),
CGAffineTransformMakeRotation(-0.785398163))
outerFrame.addSubview(runway1)
runway2.transform = CGAffineTransformConcat(CGAffineTransformMakeTranslation(120, 140),
CGAffineTransformMakeRotation(-0.585398163))
outerFrame.addSubview(runway2)
outerFrame.backgroundColor = UIColor.yellowColor()
outerFrame.clipsToBounds = true
// View these elements
runway1
outerFrame
runway1
这是为什么画出来的? – 2015-04-29 20:15:11
@Alexander:在iOS中,您需要首先翻转坐标系。 – zisoft 2015-04-30 04:19:28
'CGRect.integral'(Swift 3)或'CGRectIntegeral(rect)'(Swift 2)优于'ceil' – 2016-09-29 03:03:10