问题
I have a table view with custom cells and I try to optimize it by making all subviews inside the cells in green color when Color Blended Layer
is checked in the simulator.
When the background of an UILabel
in the cell is set to white:
let title = UILabel()
contentView.addSubview(title)
title.background = UIColor.whiteColor()
title.text = "Hi, how are you?"
This UILabel subview will become green color in the simulator, which is good. However, if I change the text to some Chinese:
title.text = "你好"
The UILabel subview will become red. This SO post provides some explanation about the situation. Is there actually a solution for this?
回答1:
Use CATextLayer
instead of UILabel
. And don't forget to set its opaque
property to true
.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
// You should definitely put layer handling logic into
// UITableViewCell subclass. I'm omitting it for clarity
let layer = cell.contentView.layer
let textLayer = CATextLayer()
textLayer.string = "你好"
textLayer.bounds = CGRect(x: 0, y: 0, width: 100, height: 50)
textLayer.position = CGPoint(x: 50, y: 25)
textLayer.opaque = true
layer.addSublayer(textLayer)
return cell
}
UPD: If a black background is not what you are looking for, a problem becomes a bit trickier, but yet solvable.
All you need is to inject two lines of code into drawing mechanism. There are two ways to achieve this:
to use a
delegate
of aCALayer
orto make a subclass of
CATextLayer
.
In our case I believe the latter is preferable. So instead of CATextLayer
in the example below use this class:
class OpaqueTextLayerWithCustomizableBackgroundColor: CATextLayer {
override func drawInContext(ctx: CGContext) {
if let color = backgroundColor {
CGContextSetFillColorWithColor(ctx, color)
CGContextFillRect(ctx, bounds);
}
super.drawInContext(ctx)
}
}
回答2:
FYI, three steps in XIB (personal preference), or in code.
- set opaque = YES
- backgroundColor = a solid color
- clipsToBound = YES
then non english UILabel will render-pass once, as green.
回答3:
I have to say this is the internal implementation of Apple. And we shouldn't / (probably) can't fiddle with it. After all, the 'colors' you see is only the representation of the structure of the layers. The answer on the question you attached is a good explanation.
I would suggest that don't plan any premature optimizations. After all, even if you do feel performance increase, the 'Color Blended Layer' is ugly and cannot be applied for real devices. Or simply try not to pay attention to it!
Edit:
Here is a screenshot from Apple's Simulator, if Apple can live with that, you can live with it too!
回答4:
Check your textlayer.string first letter is English or some other
NSMutableArray*arrAlphabets;
arrAlphabets=[NSMutableArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil]]
Then Inside Tableview cell for row at indexpath method:
NSString *str=[textlayer.string substringToIndex:1];
if(str isEqualToString:arrAlphabets) //use for loop to run arralphabets
{
//do your code to maintail the layer color .
}
Hope this concept helps you :)
回答5:
Maybe you could check for monospace characters (western) versus proportional ones (Chinese) depending on the font used.
Monospace characters are 8-bit where proportional ones are 16-bit. I'm at a loss of how Swift could check for byte sizes, so I can't provide any code.
Apple's Swift programming guide can help. Check the section titled: Unicode Representations of Strings.
来源:https://stackoverflow.com/questions/35308501/is-it-possible-to-make-uilabel-with-non-english-characters-green-when-color-blen