How to make TTButtons title left aligned? - three20

I know how to create TTButton:
TTButton *epriceButton = [TTButton buttonWithStyle:#"epriceButton:"];
And I have a method:
- (TTStyle *)epriceButton:(UIControlState)state {
TTShape* shape;
shape = [TTRoundedRectangleShape shapeWithRadius:4.5];
UIColor* tintColor = RGBCOLOR(8, 101, 191);
return [TTSTYLESHEET toolbarButtonForState:state shape:shape tintColor:tintColor font:nil];
}
That works nice. Last one thing I want from that button is to have its title left aligned. I'm new to three20 library, so I think I don't understand how styles work. I found that each TTStyle has next: method. But how multiple styles work together?

The text alignment for the button is defined inside -(TTStyle*)toolbarButtonForState: shape: tintColor: font:. So you will need to create your own version of this method.
If you look into the original you'll find a TTTextStyle as the last style being added. TTTextStyle let's you set the textAlignment with the convenient initializer
+ (TTTextStyle*)styleWithFont:(UIFont*)font color:(UIColor*)color
minimumFontSize:(CGFloat)minimumFontSize
shadowColor:(UIColor*)shadowColor shadowOffset:(CGSize)shadowOffset
textAlignment:(UITextAlignment)textAlignment
verticalAlignment:(UIControlContentVerticalAlignment)verticalAlignment
lineBreakMode:(UILineBreakMode)lineBreakMode numberOfLines:(NSInteger)numberOfLines
next:(TTStyle*)next
So your implementation might look like:
- (TTStyle*)myButtonForState:(UIControlState)state shape:(TTShape*)shape
tintColor:(UIColor*)tintColor font:(UIFont*)font {
UIColor* stateTintColor = [self toolbarButtonColorWithTintColor:tintColor forState:state];
UIColor* stateTextColor = [self toolbarButtonTextColorForState:state];
return
[TTShapeStyle styleWithShape:shape next:
[TTInsetStyle styleWithInset:UIEdgeInsetsMake(2, 0, 1, 0) next:
[TTShadowStyle styleWithColor:RGBACOLOR(255,255,255,0.18) blur:0 offset:CGSizeMake(0, 1) next:
[TTReflectiveFillStyle styleWithColor:stateTintColor next:
[TTBevelBorderStyle styleWithHighlight:[stateTintColor multiplyHue:1 saturation:0.9 value:0.7]
shadow:[stateTintColor multiplyHue:1 saturation:0.5 value:0.6]
width:1 lightSource:270 next:
[TTInsetStyle styleWithInset:UIEdgeInsetsMake(0, -1, 0, -1) next:
[TTBevelBorderStyle styleWithHighlight:nil shadow:RGBACOLOR(0,0,0,0.15)
width:1 lightSource:270 next:
[TTBoxStyle styleWithPadding:UIEdgeInsetsMake(8, 8, 8, 8) next:
[TTImageStyle styleWithImageURL:nil defaultImage:nil
contentMode:UIViewContentModeScaleToFill size:CGSizeZero next:
[TTTextStyle styleWithFont:font color:stateTextColor
minimumFontSize:14.0
shadowColor:[UIColor colorWithWhite:0 alpha:0.4] shadowOffset:CGSizeMake(0, -1)
textAlignment:UITextAlignmentLeft
verticalAlignment:UIControlContentVerticalAlignmentCenter
lineBreakMode:UILineBreakModeWordWrap numberOfLines:1
next:nil]]]]]]]]]];
}

Related

Qt Creator label value

I currently face the following problem:
I have 64 labels. Label_1 all the way up to Label_64.
I also have an int i.
"i" also goes from 1-64
I want that, when i == 1 Label_1 shall display an image. If i == 2, Label_2 shall display that image and so on.
Currently I'd do that with:
if(i == 1)
{
QPixmap pix("...");
ui->label_1->setPixmap(pix);
}
if(i == 2)
{
QPixmap pix("...");
ui->label_2->setPixmap(pix);
}
if(i == 3)
{
QPixmap pix("...");
ui->label_3->setPixmap(pix);
}
...
Is there some way to do that easier? Something like:
QPixmap pix("...");
ui->label_i->setPixmap(pix);
where the choosen label is directly defined by i?
You can store a list of QLabels.
QList<QLabel*> labels;
labels.at(i)->setPixmap(pix)
The disadvantage of this method is that you should manually assign ui->label_i to labels.at(i) for every i from 1 to 64 once:
labels.insert(0, NULL); // empty space to keep numbering the same.
labels.insert(1, ui->labels_1);
labels.insert(2, ui->labels_2);
...
labels.insert(64, ui->labels_64);
Depending on your specific case, you may use a more tricky solution. For example, if all the labels are stored in a QVBoxLayout at position 1 to 64, you can access label i as follows:
QVBoxLayout *layout = ...;
QLabel *label = qobject_cast<QWidget*> (layout->itemAt(i)->widget ());
if (label) // should be true if assumption is correct
label->setPixmap(pix);
You can also use method two to initialise the list of method 1.
See the Qt documentation for more information.

AdjustsFontSizeToFitWidth && numberOfLines = 0 doesn't work together as expected

let nameBox = UILabel(x: 0, y: 0, w: sideSize, h: sideSize*2/4)
nameBox.text = skillName
nameBox.textAlignment = .Center
nameBox.numberOfLines = 0
nameBox.adjustsFontSizeToFitWidth = true
nameBox.addBorderLeft(size: 1, color: UIColor.blackColor())
nameBox.addBorderTop(size: 1, color: UIColor.blackColor())
nameBox.addBorderRight(size: 1, color: UIColor.blackColor())
container.addSubview(nameBox)
This is the code I have and its output is below.
As you can see it has modified everything perfectly except for Communication & Lumberjack. Why is it, and how can I solve it?
Looks like you need to set the lineBreakMode of the label to .ByWordWrapping.
Another thought is that you really want the font to be smaller. Perhaps this is prevented by the minimumScaleFactor property.
From the docs for adjustsFontSizeToFitWidth:
The default value for this property is false. If you change it to true, you should also set an appropriate minimum font size by modifying the minimumFontSize property.
This seems to be a documentation bug. minimumFontSize is deprecated.

How to get inner and outer window dimensions with Xlib/XCB?

Is there a reliable way to get the inner and outer rectangle of a top
level window with XCB/Xlib? (IOW frame and client rectangle).
Here's what I tried:
xcb_get_geometry always returns the initial dimensions even after
the window gets resized (what gives?)
I figured I would call xcb_query_tree repeatedly until I find the
window's frame window - is this the way to do it? I figure ICCCM/EWMH
should provide this but couldn't find anything. Is there any other
standard/non-standard for this? Anyway that doesn't work with
compiz/ubuntu10 because xcb_query_tree reports the client window as
having root = parent (under normal ubuntu wm the window gets properly
reparented).
xcb_translate_coordinates() seemed to be the only reliable way to
get root-based coords[1] in 2007 -- is this still the case? I.e. is
XCB_CONFIGURE_NOTIFY non-standard with WMs?
[1] http://fixunix.com/xwindows/91652-finding-position-top-level-windows.html
This is a partial answer as it only explains how to find the inner dimensions of a window. Also I am not sure if this is the canonical way to go but it works for me.
You can subscribe to the XCB_EVENT_MASK_RESIZE_REDIRECT event when creating a window:
xcb_window_t window = xcb_generate_id (connection);
const xcb_setup_t *setup = xcb_get_setup (connection);
xcb_screen_t *screen = xcb_setup_roots_iterator (setup).data;
uint32_t mask = XCB_CW_EVENT_MASK;
uint32_t valwin[1] = { XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_RESIZE_REDIRECT };
xcb_create_window(
connection,
XCB_COPY_FROM_PARENT,
window,
screen->root,
0, 0,
800, 600,
0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
screen->root_visual,
mask, valwin);
xcb_map_window(connection, window);
xcb_flush(connection);
In the event loop you can then keep track of resizes:
xcb_generic_event_t *event;
uint16_t width = 0, height = 0;
while ((event = xcb_wait_for_event(connection)) != NULL) {
switch (event->response_type & ~0x80) {
case XCB_EXPOSE: {
/* ... */
break;
}
case XCB_RESIZE_REQUEST: {
auto resize = (xcb_resize_request_event_t*) event;
if (resize->width > 0) width = resize->width;
if (resize->height > 0) height = resize->height;
break;
}
default:
break;
}
free(event);
xcb_flush(connection);
}
Note that I am not sure whether this event is emitted when you initiate the resize from your application code using xcb_configure_window for example. I never tested it and just update the width and height in a wrapper function for xcb_configure_window.

Error: Use of unresolved identifier 'kCGBlendModeMultiply'

I recently updated to Xcode 7, beta 3.
And I've run into some issues, I can't seem to find any questions for on SO.
When I run my application, i get 3 errors:
Use of unresolved identifier 'kCGBlendModeMultiply'
Use of unresolved identifier 'kCGLineCapRound'
Use of unresolved identifier 'kCGLineJoinMiter'
However the 2 latter ones, disappear, although I assume they will show up after the first one is fixed (hence why I included it in this question).
I didn't see anything in the release notes about these being removed? So I'm a bit stuck at what to do. I tried rewriting the lines of course, but the 3 things I used don't show up as options anymore. In case that they are just gone in the latest Swift 2.0, what can I use instead?
Here's the code for the first error.
func alpha(value:CGFloat)->UIImage
{
UIGraphicsBeginImageContextWithOptions(self.size, false, 0.0)
let ctx = UIGraphicsGetCurrentContext()
let area = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height)
CGContextSetBlendMode(ctx, kCGBlendModeMultiply)
CGContextSetAlpha(ctx, value)
CGContextDrawImage(ctx, area, self.CGImage)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage;
}
Here's the code for the 2 latter errors:
for layer in [ self.top, self.middle, self.bottom ] {
layer.fillColor = nil
layer.strokeColor = UIColor.whiteColor().CGColor
layer.lineWidth = 4
layer.miterLimit = 4
layer.lineCap = kCALineCapRound
layer.masksToBounds = true
let strokingPath = CGPathCreateCopyByStrokingPath(layer.path, nil, 4, kCGLineCapRound, kCGLineJoinMiter, 4)
layer.bounds = CGPathGetPathBoundingBox(strokingPath)
layer.actions = [
"strokeStart": NSNull(),
"strokeEnd": NSNull(),
"transform": NSNull()
]
self.layer.addSublayer(layer)
}
Any help would be greatly appreciated! :)
This should work:
CGContextSetBlendMode(ctx, CGBlendMode.Multiply)
... or even just this:
CGContextSetBlendMode(ctx, .Multiply)
If you Ctrl-click on CGContextSetBlendMode and then from its declaration jump (in the same way) to declaration of CGBlendMode then you will see:
enum CGBlendMode : Int32 {
/* Available in Mac OS X 10.4 & later. */
case Normal
case Multiply
case Screen
case Overlay
// ...
}
Similarly, the other line that produces the error should be changed to:
let strokingPath = CGPathCreateCopyByStrokingPath(layer.path, nil, 4, .Round, .Miter, 4)

Offset position of "close button" in Three20 TTLauncherView

I'm using the Three20 library to create a TTLauncherView in an iPhone app that I'm working on. When the user touches and holds on one of the items in the launcher view it causes all the items to start wiggling and an "x" button to appear in the top left corner of the of each item image. This button is used to remove items from the launcher view. I would like to move this "x" button a little down and to the right relative to the item image.
I have looked at TTDeafaultStyleSheet andfound the following code:
///////////////////////////////////////////////////////////////////////////////////////////////////
- (TTStyle*)launcherCloseButtonImage:(UIControlState)state {
return
[TTBoxStyle styleWithMargin:UIEdgeInsetsMake(-2, 0, 0, 0) next:
[TTImageStyle styleWithImageURL:nil defaultImage:nil contentMode:UIViewContentModeCenter
size:CGSizeMake(10,10) next:nil]];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (TTStyle*)launcherCloseButton:(UIControlState)state {
return
[TTShapeStyle styleWithShape:[TTRoundedRectangleShape shapeWithRadius:TT_ROUNDED] next:
[TTInsetStyle styleWithInset:UIEdgeInsetsMake(1, 1, 1, 1) next:
[TTShadowStyle styleWithColor:RGBACOLOR(0,0,0,0.5) blur:2 offset:CGSizeMake(0, 3) next:
[TTSolidFillStyle styleWithColor:[UIColor blackColor] next:
[TTInsetStyle styleWithInset:UIEdgeInsetsMake(-1, -1, -1, -1) next:
[TTSolidBorderStyle styleWithColor:[UIColor whiteColor] width:2 next:
[TTPartStyle styleWithName:#"image" style:TTSTYLE(launcherCloseButtonImage:) next:
nil]]]]]]];
}
This code touches the "x" button, but I haven't been able to figure out how to change its position. Does anyone have any suggestions?
I was unable to figure our how to move the close button in the way that I wanted using the code in my question, but I did find where to adjust the position of the button by editing the Three20 file TTLauncherButton.m. In the -(void)layoutSubviews method I adjusted the origin of the _closeButton property.

Resources