我想在xml文件中创建一个带有背景图像的Android ImageButton,并在其中间放置一个较小的图标,位于中心.出于某种原因,如何做到这一点并不明显,文档也无济于事.
<ImageButton
android:id="@+id/sound_button"
android:layout_x="430px"
android:layout_y="219px"
android:layout_width="48px"
android:layout_height="48px"
android:scaleType="center"
android:src="@android:drawable/volumeicon"
android:background="@drawable/clearbuttonup"
/>
Run Code Online (Sandbox Code Playgroud)
但是,无论我尝试什么(在layout _ height和layout _ width上放置" wrap _ content "而不是绝对数字,图标仍保留在按钮的左上角.但是,如果我没有背景图像(即一个系统默认的白色按钮),图标会进入中心.当我使用颜色而不是图像作为背景时,按钮也会进入左上角.
为什么会发生这种情况,我将如何实际获得我想要的行为 - 也就是说,中心带有图标的背景图像?
我正在使用UITableViewCell和UITableViewCellStyleSubtitle.但是,由于我用于单元格的背景图像,我不喜欢detailTextLabel的去向.我想移动它并在单元格内调整大小,但我尝试的任何东西似乎都没有用.
CGRect currRect = cell.detailTextLabel.frame;
cell.detailTextLabel.frame = CGRectMake(currRect.origin.x + 20, currRect.origin.y, currRect.size.width - 40, currRect.size.height);
Run Code Online (Sandbox Code Playgroud)
我尝试将此代码放入tableView:cellForRowAtIndexPath和tableView:willDisplayCell:forRowAtIndexPath方法,但两者都不起作用.标签只是保持原样,大小相同.
调整标签的其他属性(textAlignment,backgroundColor,当然还有文本本身)可以正常工作.
如何让detailTextLabel移动到我需要的位置?
如果这很重要(虽然我不明白为什么它应该),这个特定的单元格也有imageView.image和backgroundImage属性设置.两个图像都正确显示.
如果你正在实现一个子类,你可以在你的实现中显式调用超类的方法,即使你已经覆盖了那个方法,即:
[self overriddenMethod]; //calls the subclass's method
[super overriddenMethod]; //calls the superclass's method
Run Code Online (Sandbox Code Playgroud)
如果你想从子类的实现之外的某个地方调用超类的方法,即:
[[object super] overriddenMethod]; //crashes
Run Code Online (Sandbox Code Playgroud)
这甚至可能吗?而且,通过扩展,是否有可能在实现中超过一个级别,即:
[[super super] overriddenMethod]; //will this work?
Run Code Online (Sandbox Code Playgroud) 目标:取一个UIImage,在中间裁剪出一个正方形,将正方形的大小改为320x320像素,将图像切割成16个80x80图像,将16个图像保存在一个数组中.
这是我的代码:
CGImageRef originalImage, resizedImage, finalImage, tmp;
float imgWidth, imgHeight, diff;
UIImage *squareImage, *playImage;
NSMutableArray *tileImgArray;
int r, c;
originalImage = [image CGImage];
imgWidth = image.size.width;
imgHeight = image.size.height;
diff = fabs(imgWidth - imgHeight);
if(imgWidth > imgHeight){
resizedImage = CGImageCreateWithImageInRect(originalImage, CGRectMake(floor(diff/2), 0, imgHeight, imgHeight));
}else{
resizedImage = CGImageCreateWithImageInRect(originalImage, CGRectMake(0, floor(diff/2), imgWidth, imgWidth));
}
CGImageRelease(originalImage);
squareImage = [UIImage imageWithCGImage:resizedImage];
if(squareImage.size.width != squareImage.size.height){
NSLog(@"image cutout error!");
//*code to return to main menu of app, irrelevant here
}else{
float newDim = squareImage.size.width;
if(newDim …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个静态库,将其分发给可能需要调试语句的其他开发人员.所以我有几个级别的日志记录.
为了避免不断出现
if(loggingLevelCurrentlySet >= loggingLevelWantedForThisInstance){
NSLog(@"log this");
}
Run Code Online (Sandbox Code Playgroud)
我创建了一组日志功能包装器.简化版本如下所示:
void myLog(int logLevel, NSString *format, va_list args){
if((loggingLevelCurrentlySet >= logLevel)){
NSLogv(format, args);
}
}
void myLogLevel1(NSString *format, ...){
va_list args;
va_start(args, format);
myLog(1, format, args);
va_end(args);
}
void myLogLevel2(NSString *format, ...){
va_list args;
va_start(args, format);
myLog(2, format, args);
va_end(args);
}
Run Code Online (Sandbox Code Playgroud)
等等
但是现在,我希望从myLog中访问完全格式化的字符串以执行其他操作.
void myLog(int logLevel, NSString *format, va_list args){
NSString *fullString = [NSString stringWithFormat:format, args]; //crashes when args is anything but an empty list
CFStringRef cfsr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, format, args); …
Run Code Online (Sandbox Code Playgroud) 我一直在寻找一种方法来使用可选的协议方法并拥有干净的代码.换句话说:
1:respondsToSelector:
我的代码没有调用
2.应该适用于任何方法签名,因此NSObject上的类别方法进行检查和调用performSelector:
(NSInvocation与ARC合作有问题)
3:此解决方案,IMO,假装是普遍的但具有1的所有缺点
我最终提出了这个想法:
@protocol MyProtocol <NSObject>
@optional
-(void)optionalMethod;
@end
@interface ClassA : NSObject <MyProtocol>
@end
@implementation ClassA
-(void)optionalMethod{
NSLog(@"ClassA implements optionalMethod");
}
@end
@interface ClassB : NSObject <MyProtocol>
@end
@implementation ClassB
//classB does not implement optionalMethod
@end
@interface NSObject (DefaultMyProtocolImplementation)
-(void)optionalMethod;
@end
@implementation NSObject (DefaultMyProtocolImplementation)
-(void)optionalMethod{
NSLog(@"%@ does not implement optionalMethod", NSStringFromClass([self class]));
}
@end
Run Code Online (Sandbox Code Playgroud)
它似乎工作,即:
...
ClassA *objA = [[ClassA alloc] init];
ClassB *objB = [[ClassB alloc] init];
[objA optionalMethod]; //prints …
Run Code Online (Sandbox Code Playgroud) protocols objective-c objective-c-runtime ios objective-c-category
我离开Ruby on Rails开发已有一段时间了,经历了几次系统更新。这是在Mac OS X Sierra及其系统版本的Ruby(2.0.0p648)和Rails 4.0.2上发生的。
rails new appname
当appname
目录不存在时,创建一个新的应用程序()效果很好-该命令创建了该应用程序,并按需填充该应用程序。但是,尽管所有指南和教程都说了那样,但当存在这样的目录(即使它为空)时,它也会失败。您也无法创建一个空目录,将其cd进入并运行rails new .
失败命令的输出如下所示:
存在于/Library/Ruby/Gems/2.0.0/gems/thor中的
/Library/Ruby/Gems/2.0.0/gems/railties-4.0.2/lib/rails/generators/app_base.rb:97:在create_root': uninitialized constant Rails::Generators::AppBase::FileUtils (NameError) from (eval):1:in
create_root中-0.20.0 / LIB /托尔/ command.rb:27:在run' from /Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in
invoke_command”从/Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:inblock in invoke_all' from /Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in
每个'来自/Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:inmap' from /Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in
invoke_all'来自/Library/Ruby/Gems/2.0.0/gems/thor- 0.20.0 / lib / thor / group.rb:232:dispatch' from /Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/base.rb:466:in
从/Library/Ruby/Gems/2.0.0/gems/railties-4.0.2/lib/rails/commands/application.rb:43:in开始<top (required)>' from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:68:in
要求'来自/Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:68:inrequire' from /Library/Ruby/Gems/2.0.0/gems/railties-4.0.2/lib/rails/cli.rb:15:in
'来自'来自/Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:68: 在require' from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:68:in
要求'来自/Library/Ruby/Gems/2.0.0/gems/railties-4.0.2/bin/rails:9:in<top (required)>' from /usr/bin/rails:22:in
load'来自/ usr / bin / rails:22:in`' …
objective-c ×5
iphone ×4
cocoa-touch ×3
android ×1
cocoa ×1
imagebutton ×1
ios ×1
layout ×1
macos ×1
nsstring ×1
overriding ×1
protocols ×1
ruby ×1
rubygems ×1
superclass ×1
uitableview ×1
xml ×1